如何让jq输出很长的数据集?

问题描述 投票:0回答:1

假设我有这个 ndjson:

{"id": "one", "colors": [{"color": "blue"}, {"color": "red"}]}
{"id": "two", "colors": [{"color": "green"}]}

如何获得下面的输出?

one blue
one red
two green

这是一次失败的尝试:

$ cat tmp/test.json  | jq -r '.id, .colors[].color'
one
blue
red
two
green

这是第二次失败的尝试:

$ cat tmp/test.json  | jq -r '[.id, .colors[].color]|@tsv'
one blue    red
two green
jq
1个回答
0
投票

combinations
函数迭代其输入元素的所有组合。这会将每个
id
与每个
color
配对:

$ jq -r '[[.id], [.colors[].color]] | combinations | join(" ")' test.json
one blue
one red
two green
© www.soinside.com 2019 - 2024. All rights reserved.