我正在寻找一个可以解决 JSON 文件问题的脚本。问题是 API 正在将此文件传递给我:
{
"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"
]
}
},
...
}
我需要修改它,使文件中的每个设备如下:
{
"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
尝试
jq 'with_entries(.value|=with_entries(.value|=([.descriptions, .values] | transpose | map({description:.[0], value:.[1]}))))' < yourfile.json
第一个with_entries
迭代所有设备键,使用 修改它们的值.value|=...
。然后每个设备的数据字段也相同。最后,我们获取描述和值列表,对它们进行转置,使每个值与描述配对,然后提取它们,使用description
和value
字段构建一个对象。
您可以将其嵌入到任何 shell 脚本等中。
答案2
假设 JSON 文档格式良好,并且每个descriptions
条目values
对于顶级device 1
键下的每个子对象都有一个对应的条目(反之亦然):
jq '."device 1"[] |=
[
range(.values|length) as $i |
{ description: .descriptions[$i], value: .values[$i] }
]' file.json
通过迭代这些数组的索引并为每对创建一个新对象,将descriptions
值和值配对。values
这些新对象被放入一个数组中,然后替换 下当前子对象的旧结构device 1
。