假设我有这个 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
combinations
函数迭代其输入元素的所有组合。这会将每个 id
与每个 color
配对:
$ jq -r '[[.id], [.colors[].color]] | combinations | join(" ")' test.json
one blue
one red
two green