【发布时间】:2020-05-09 18:43:22
【问题描述】:
我的 JSON 看起来像这样:
test.json
[
{
"item1": {
"item2": 123,
"array1": [
{
"item3": 456,
"item4": "teststring"
},
{
"item3": 789,
"item4": "teststring2"
}
]
}
}
]
我正在尝试按以下格式打印出来:
123, 456, teststring
123, 789, teststring2
我试着这样做:
cat test.json | jq -r '.[].item1.item2, (.[].item1.array1[] | .item3, .item4)' | xargs printf "%s, %s, %s\n"
但是结果是item2被打印出来了,接着是第一行item3和item4,然后下一行只打印item3和item4,像这样:
123, 456, teststring
789, teststring2,
如何为从数组内打印的每组元素打印数组外部的元素?
【问题讨论】:
-
你想要
123, 789, teststring还是123, 789, teststring2? -
第二行的Teststring2,感谢发现我更正了它
-
数组中的对象中item3/item4键的顺序会一直一样吗?
-
@peak 是的,顺序总是一样的
标签: json export-to-csv jq