Scripting
修改 JSON 文件結構的腳本
我正在尋找一個可以解決我在使用 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"} ] }, ... }
嘗試
jq 'with_entries(.value|=with_entries(.value|=([.descriptions, .values] | transpose | map({description:.[0], value:.[1]}))))' < yourfile.json
第一個
with_entries
迭代所有設備鍵,用 修改它們的值.value|=...
。然後對於每個設備的數據欄位也是如此。最後,我們獲取描述和值列表,將它們轉置,使每個值都與描述配對,然後提取它們,建構一個包含description
和value
欄位的對象。您可以將其嵌入到任何 shell 腳本等中。