Turn an array into an object (INDEX)
Build an object keyed by the id field
jq 'INDEX(.[]; .id)' items.json
More jq commands
-
jq -s '.[0] * .[1]' base.json override.jsonDeep-merge two JSON files. Values from override.json win
-
jq '.config?.database?.host // "localhost"' config.jsonThe ? operator avoids errors if a step is not an object, and // falls back to localhost
-
jq 'sort_by(.timestamp) | reverse | .[0:5]' events.jsonSort by timestamp, newest first, and take the first 5
-
jq -r '.[] | [.id, .name, .status] | @csv' data.jsonPrint id, name and status as CSV lines
-
jq '[.[] | . + {"env": "prod"}]' items.jsonAdd env: prod to every array item