Skip to content

Instantly share code, notes, and snippets.

@knudmoeller
Last active December 4, 2018 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knudmoeller/31cf9a102398e2fd87b0c696d187082e to your computer and use it in GitHub Desktop.
Save knudmoeller/31cf9a102398e2fd87b0c696d187082e to your computer and use it in GitHub Desktop.
Add an index to an array of JSON objects with jq

Add an index field to an array of objects

input data.json:

[
  {
    "foo": "bar"
  } ,
  {
    "foo": "baz"
  } ,
  {
    "foo": "dingo"
  }
]
cat data.json | jq 'to_entries | map({ "id": .key} + .value)'

output:

[
  {
    "id": 0 ,
    "foo": "bar"
  } ,
  {
    "id": 1 ,
    "foo": "baz"
  } ,
  {
    "id": 2 ,
    "foo": "dingo"
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment