Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to transform these 2 arrays by using jq, into a set of objects, like in the example down below?

Example json data:

{
  "data": [
    {
      "place": "FM346",
      "id": [
        "7_day_A",
        "7_day_B",
        "7_day_C",
        "7_day_D"
      ],
      "values": [
        0,
        30,
        23,
        43
      ]
    },
    {
      "place": "LH210",
      "id": [
        "1_day_A",
        "1_day_B",
        "1_day_C",
        "1_day_D"
      ],
      "values": [
        4,
        45,
        100,
        9
      ]
    }
  ]
}

what i need to transform it into:

{
  "data": [
    {
      "place": "FM346",
      "7_day_A": {
        "value": 0
      },
      "7_day_B": {
        "value": 30
      },
      "7_day_C": {
        "value": 23
      },
      "7_day_D": {
        "value": 43
      }
    },
    {
      "place": "LH210",
      "1_day_A": {
        "value": 4
      },
      "1_day_B": {
        "value": 45
      },
      "1_day_C": {
        "value": 100
      },
      "1_day_D": {
        "value": 9
      }
    }
  ]
}

i have tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    data:[.data |.[]|
    {   
        place: (.place),
        (.id[]): 
        {
            value: (.values[])
        }
    }] 
}

(in jqplay: https://jqplay.org/s/f4BBtN9gwmp)

and this:

{
    data:[.data |.[]|
    {   
        place: (.place),
        test:
        [{
            (.id[]): 
            {
            value: (.values[])
            }
        }]
    }] 
}

(in jqplay: https://jqplay.org/s/pKIvQe1CzgX)

but they arent grouped in the way i wanted and it gives each value to each id, not the corresponding one.
I have been trying for some time now, but im new to jq and have no idea how to transform it this way, thanks in advance for any answers.

>Solution :

You can use transpose here, which can play a key role in converting the arrays to key/value pairs

.data[] |= {place} + ( [ .id, .values ] | transpose | map( {(.[0]): { value: .[1] } }) | add )

The solution works by converting the array-of-arrays [.id, .values] by transposing them, i.e. converting

[["7_day_A","7_day_B","7_day_C","7_day_D"],[0,30,23,43]]
[["1_day_A","1_day_B","1_day_C","1_day_D"],[4,45,100,9]]

to

[["7_day_A",0],["7_day_B",30],["7_day_C",23],["7_day_D",43]]
[["1_day_A",4],["1_day_B",45],["1_day_C",100],["1_day_D",9]]

With the transformation done, we construct an object with key as the zeroth index element and value as an object comprising of the value of first index element, and combine the results together with add

Demo – jqplay

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading