简体   繁体   English

如何使用 jq 将值与 JSON 中另一个数组的每个元素组合在一起?

[英]how to combine a value with each of the element of another array in a JSON using jq?

The JSON like this: JSON 像这样:

[
  {
    "id": 1,
    "names": [
      "apple",
      "google"
    ]
  },
  {
    "id": 2,
    "names": [
      "iphone",
      "ipad",
      "macbook"
    ]
  }
]

expected output in tsv tsv 中的预期 output

1 apple
1 google
2 iphone
2 ipad
2 macbook

You can use map您可以使用map

along with raw-output option such as以及原始输出选项,例如

jq -r 'map( "\(.id) " + .names[] )[]'

Demo演示

or formatting as tsv :或格式化为tsv

jq -r 'map( "\(.id) " + .names[] ) | @tsv'

which outputs the result on a single line在单行上输出结果

Demo演示

or use或使用

jq -r 'map( "\(.id)\t" + .names[])[]'

in order to get tab-delimited results between attributes while returning each combinations on separate lines为了在属性之间获得制表符分隔的结果,同时在单独的行上返回每个组合

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM