Getting one value out is the most common jq task. Chain keys with dots and index arrays with
[n].
Example input
{
"user": { "name": "Ada", "roles": ["admin", "dev"] },
"active": true
}
Top-level and nested keys
jq '.active' data.json # true
jq '.user.name' data.json # "Ada"
Array elements
jq '.user.roles[0]' data.json # "admin"
jq '.user.roles[-1]' data.json # "dev" (negative = from the end)
Keys with spaces or special characters
Quote them:
jq '.["first name"]' data.json
Avoid “Cannot index null with …” on missing keys
Indexing into a missing/null path errors. Add ? to make it return null (or nothing) instead:
jq '.user.address.city?' data.json # no error if address is missing
Provide a fallback with the alternative operator //:
jq '.user.nickname // "anonymous"' data.json
Get the keys of an object
jq 'keys' data.json # ["active","user"] (sorted)
jq 'keys_unsorted' data.json # original order
? vs //: ? suppresses the error from a bad path; // supplies a value when the left
side is null or false. Combine them — .a.b? // "default" — for “safe access with a fallback”.