skip to Main Content

I have a file with an array of JSON:

[
  {
    "names": [
      "name1"
    ],
    "id": "1"
  },
  {
    "names": [
      "name1",
      "name2"
    ],
    "id": "2"
  }
]

and I would like to create a new file with each JSON of previous JSON array, broken down by its field "origin", like this:

[
  {
    "name": "name1"
    "id": "1"
  },
  {
    "name": "name1",
    "id": "2"
  },
  {
    "name": "name2",
    "id": "2"
  }
]

I use jq version 1.6 in a bash script.

2

Answers


  1. Here is one way:

    map({id, name: .names[]})
    

    Online demo

    Login or Signup to reply.
  2. Try this:

    jq '[.[] | .names[] as $n | {id: .id, name: $n}]' input.json > output.json  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search