Read a nested value safely
The ? operator avoids errors if a step is not an object, and // falls back to localhost
jq '.config?.database?.host // "localhost"' config.json
More jq commands
-
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
-
jq 'walk(if type == "string" then ascii_downcase else . end)' data.jsonConvert every string value in the document to lowercase
-
cat data.json | jq '.'Format and colorize a JSON file