Convert array to object (index_by)

Build an object keyed by the id field

jq 'INDEX(.[]; .id)' items.json

tool
jq17 commands
tags
jq

More jq commands

  • jq -s '.[0] * .[1]' base.json override.json

    Deep merge two JSON objects (right wins on conflicts)

  • jq '.config?.database?.host // "localhost"' config.json

    Optional chaining with fallback default value

  • jq 'sort_by(.timestamp) | reverse | .[0:5]' events.json

    Get 5 most recent events sorted by timestamp descending

  • jq -r '.[] | [.id, .name, .status] | @csv' data.json

    Output selected fields as comma-separated values

  • jq '[.[] | . + {"env": "prod"}]' items.json

    Inject a new key-value pair into every array element

all 17 jq commands