[英]Use JQ to output JSON nested object into array, before conversion to CSV
Use JQ to output JSON nested object into array, before conversion to CSV使用 JQ 将 output JSON 嵌套 object 到数组中,然后转换为 ZCC8D68C551C4A9A6DZD5313E
Question is an extension of previous solution:问题是先前解决方案的扩展:
Use JQ to parse JSON array of objects, using select to match specified key-value in the object element, then convert to CSV 使用JQ解析JSON对象数组,使用select匹配object元素中的指定键值,然后转换为object元素,然后转换为select
Data Source:数据源:
{
"Other": [],
"Objects": [
{
"ObjectElementName": "Test 123",
"ObjectElementArray": [],
"ObjectNested": {
"0": 20,
"1": 10.5
},
"ObjectElementUnit": "1"
},
{
"ObjectElementName": "Test ABC 1",
"ObjectElementArray": [],
"ObjectNested": {
"0": 0
},
"ObjectElementUnit": "2"
},
{
"ObjectElementName": "Test ABC 2",
"ObjectElementArray": [],
"ObjectNested": {
"0": 15,
"1": 20
},
"ObjectElementUnit": "5"
}
],
"Language": "en-US"
}
JQ command to extract [FAILS] JQ 命令提取 [失败]
jq -r '.Objects[]
| select(.ObjectElementName | test("ABC"))
| [.ObjectElementName,.ObjectNested,.ObjectElementUnit]
|@csv' input.json
Output CSV required (or variation, so long as ObjectNested appears into a single column in CSV)需要 Output CSV(或变体,只要 ObjectNested 出现在 CSV 中的单个列中)
ObjectElementName,ObjectNested,ObjectElementUnit
"Test ABC 1","0:0","2"
"Test ABC 2","0:15,1:20","5"
With keys_unsorted
and string interpolation, it's easy to turn ObjectNested
into the form you desired:使用keys_unsorted
和字符串插值,很容易将ObjectNested
转换为您想要的形式:
.Objects[] | select(.ObjectElementName | index("ABC")) | [
.ObjectElementName,
([.ObjectNested | keys_unsorted[] as $k | "\($k):\(.[$k])"] | join(",")),
.ObjectElementUnit
] | @csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.