jq commands

17 commands, each with a one-line explanation. Click a command to copy it, or its title for details.

Commands 17 shown

  • cat data.json | jq '.'

    Format and colorize a JSON file

  • cat data.json | jq '.items[] | select(.status == "Running")'

    Select objects where a field equals a value

  • cat data.json | jq '[.items[] | {name:.metadata.name, ns:.metadata.namespace}]'

    Project only specific fields into a new array

  • cat data.json | jq 'group_by(.namespace) | map({ns: .[0].namespace, count: length})'

    Aggregate items by a field value

  • cat data.json | jq 'del(.metadata.managedFields)'

    Remove a field from JSON output

  • cat data.json | jq -r '.items[].metadata.name'

    Raw output (-r) for scripting; one value per line

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

    Deep-merge two JSON files (override wins)

  • jq '[.[] | select(.value != null)]' data.json

    Filter array elements where value is not null

  • jq 'group_by(.status) | map({status: .[0].status, count: length})' data.json

    Count records grouped by status field

  • jq 'flatten(1)' nested.json

    Flatten one level of nested arrays

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

    Build an object keyed by the id field

  • 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

  • jq 'walk(if type == "string" then ascii_downcase else . end)' data.json

    Recursively lowercase all string values in a JSON document