jq

17 commands

17 shown

Pretty print JSON Format and colorize a JSON file cat data.json | jq '.' #jq Filter by field Select objects where a field equals a value cat data.json | jq '.items[] | select(.status == "Running")' #jq Extract specific fields Project only specific fields into a new array cat data.json | jq '[.items[] | {name:.metadata.name, ns:.metadata.namespace}]' #jq Group by field Aggregate items by a field value cat data.json | jq 'group_by(.namespace) | map({ns: .[0].namespace, count: length})' #jq Delete a field Remove a field from JSON output cat data.json | jq 'del(.metadata.managedFields)' #jq Convert array to lines Raw output (-r) for scripting; one value per line cat data.json | jq -r '.items[].metadata.name' #jq Merge two JSON objects Deep-merge two JSON files (override wins) jq -s '.[0] * .[1]' base.json override.json #jq Select non-null fields Filter array elements where value is not null jq '[.[] | select(.value != null)]' data.json #jq Group by field Count records grouped by status field jq 'group_by(.status) | map({status: .[0].status, count: length})' data.json #jq Flatten nested array Flatten one level of nested arrays jq 'flatten(1)' nested.json #jq Convert array to object (index_by) Build an object keyed by the id field jq 'INDEX(.[]; .id)' items.json #jq Merge two objects Deep merge two JSON objects (right wins on conflicts) jq -s '.[0] * .[1]' base.json override.json #jq Extract nested value safely Optional chaining with fallback default value jq '.config?.database?.host // "localhost"' config.json #jq Sort by numeric field Get 5 most recent events sorted by timestamp descending jq 'sort_by(.timestamp) | reverse | .[0:5]' events.json #jq Convert JSON to CSV Output selected fields as comma-separated values jq -r '.[] | [.id, .name, .status] | @csv' data.json #jq Add field to every element Inject a new key-value pair into every array element jq '[.[] | . + {"env": "prod"}]' items.json #jq Walk and transform all strings Recursively lowercase all string values in a JSON document jq 'walk(if type == "string" then ascii_downcase else . end)' data.json #jq
no commands match