Home > Mobile >  how to combine a value with each of the element of another array in a JSON using jq?
how to combine a value with each of the element of another array in a JSON using jq?

Time:06-27

The JSON like this:

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

expected output in tsv

1 apple
1 google
2 iphone
2 ipad
2 macbook

CodePudding user response:

You can use map

along with raw-output option such as

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

Demo

or formatting as tsv :

map( "\(.id) "   .names[] ) | @tsv

Demo

  • Related