1

I'm looking for a script that can solve a problem I'm having with a JSON file. The thing is that an API is delivering this file to me:

{
   "device 1":{
      "general_data":{
         "descriptions":[
            "description1",
            "description2",
            "description3",
            "description4",
            "description5",
            "description6",
            "description7",
            "description8"
         ],
         "values":[
            "value1",
            "value2",
            "value3",
            "value4",
            "value5",
            "value6",
            "value7",
            "value8"
         ]
      },
      "alarms":{
         "descriptions":[
            "description1",
            "description2",
            "description3",
            "description4",
            "description5",
            "description6",
            "description7",
            "description8",
            "description9"
         ],
         "values":[
            "value1",
            "value2",
            "value3",
            "value4",
            "value5",
            "value6",
            "value7",
            "value8",
            "value9"
         ]
      },
      "communications":{
         "descriptions":[
            "description1",
            "description2"
         ],
         "values":[
            "value1",
            "value2"
         ]
      },
      "status":{
         "descriptions":[
            "description1",
            "description2",
            "description3",
            "description4",
            "description5",
            "description6",
            "description7",
            "description8",
            "description9",
            "description10",
            "description11",
            "description12"
         ],
         "values":[
            "value1",
            "value2",
            "value3",
            "value4",
            "value5",
            "value6",
            "value7",
            "value8",
            "value9",
            "value10",
            "value11",
            "value12"
         ]
      },
      "measures":{
         "descriptions":[
            "description1"
         ],
         "values":[
            "value1"
         ]
      },
      "analogic_measures":{
         "descriptions":[
            "description1"
         ],
         "values":[
            "value1"
         ]
      },
      "lat_lon":{
         "descriptions":[
            "description1",
            "description2"
         ],
         "values":[
            "value1",
            "value1"
         ]
      }
   },

...

}

And I need to modify it so that each device in the file is as follows:

{
   "device 1":{
      "general_data":[
         {"description":"description1","value":"value1"},
         {"description":"description2","value":"value2"},
         {"description":"description3","value":"value3"},
         {"description":"description4","value":"value4"},
         {"description":"description5","value":"value5"},
         {"description":"description6","value":"value6"},
         {"description":"description7","value":"value7"},
         {"description":"description8","value":"value8"}
      ],
      "alarms":[
         {"description":"description1","value":"value1"},
         {"description":"description2","value":"value2"},
         {"description":"description3","value":"value3"},
         {"description":"description4","value":"value4"},
         {"description":"description5","value":"value5"},
         {"description":"description6","value":"value6"},
         {"description":"description7","value":"value7"},
         {"description":"description8","value":"value8"},
         {"description":"description9","value":"value9"}
      ],
      "communications":[
         {"description":"description1","value":"value1"},
         {"description":"description2","value":"value2"}
      ],
      "status":[
         {"description":"description1","value":"value1"},
         {"description":"description2","value":"value2"},
         {"description":"description3","value":"value3"},
         {"description":"description4","value":"value4"},
         {"description":"description5","value":"value5"},
         {"description":"description6","value":"value6"},
         {"description":"description7","value":"value7"},
         {"description":"description8","value":"value8"},
         {"description":"description9","value":"value9"},
         {"description":"description10","value":"value10"},
         {"description":"description11","value":"value11"},
         {"description":"description12","value":"value12"}
      ],
      "measures":[
         {"description":"description1","value":"value1"}
      ],
      "analogic_measures":[
         {"description":"description1","value":"value1"}
      ],
      "lat_lon":[
         {"description":"description1","value":"value1"},
         {"description":"description2","value":"value2"}
      ]
   },

...

}
  • 1
    It's much simpler to use command line tools like jq inside the script for this job. Is this an option? – dirkt Jan 20 at 16:57
  • @dirkt it would serve me but I dont know how I can do it, I was watching but nothing convinced me – Kevimuxx69 Jan 20 at 22:00
1

Try

jq 'with_entries(.value|=with_entries(.value|=([.descriptions, .values] | transpose | map({description:.[0], value:.[1]}))))' < yourfile.json 

The first with_entries iterates over all device keys, modifying their value with .value|=.... Then the same for the data fields of each device. Finally, we take the descriptions and values lists, transpose them so each value is paired to the description, and extract them, building an object with description and value fields.

You can embed that in any shell script etc.

  • Thanks man, I appreciate it. It served me for what I wanted. – Kevimuxx69 Jan 21 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.