jq Cheat Sheet: Filters, Arrays, Objects and Examples
Checked against jq 1.8.2 on 28 November 2026. Each section links to the official docs it was checked against.
jq is the command-line tool you reach for when an API or a log hands you JSON and you need one field, a filtered list or a CSV. The manual we checked is for jq 1.8, and the latest release is 1.8.2 (20 June 2026). We ran every example here with jq 1.7.1, which still ships on macOS; rows that need 1.8 are marked untested.
When to use what
Most jq work is three moves: walk into the data with .a.b[], keep what you want with select(...), and reshape it with {...} or map(...). Learn those before anything else.
Use -r whenever the output goes to another shell command; without it you get quoted strings. Use -c when you want one JSON value per line (easy to grep or feed into while read). Reach for -s only when the input is many separate JSON values and you need them as one array; on a single big document it just wraps it in [...].
Never splice shell variables into the filter string. Pass strings with --arg and numbers, booleans or JSON with --argjson; it avoids quoting bugs and injection. For deep or unknown shapes, paths and getpath beat guessing. If you just want to look at a document and click through it, our JSON formatter is faster, and the JSON path finder tells you the path to a value you can see but can't name.
Examples use a file u.json holding {"users":[...]} with three users (name, age, role, tags).
jq '.users[0].name' u.jsonPull one nested field; a missing key gives null, not an error
jq -r '.users[].name' u.jsonPrint every user's name as a bare line with no quotes
jq '.users[1:]' u.jsonSlice an array from index 1 to the end
jq '.users[0].email // "none"' u.jsonFall back to a default when a field is null or missing
jq '.users | length' u.jsonCount elements in an array (or keys in an object)
jq '.' u.jsonPretty-print the whole document with colour
jq -c '.users | map(select(.age > 20) | .name)' u.jsonKeep users older than 20 and return just their names
jq '[.users[] | select(.role == "dev")] | length' u.jsonCount the users whose role is dev
jq -c '.users | sort_by(.age) | reverse | .[0].name' u.jsonFind the oldest user by sorting on age descending
jq -c '.users | group_by(.role) | map({role: .[0].role, n: length})' u.jsonGroup users by role and count each group
jq -c '.users | unique_by(.role) | map(.name)' u.jsonKeep the first user of each distinct role
jq -c '[.users[].tags[]]' u.jsonFlatten every user's tags into one array
jq '.users | any(.age < 20)' u.jsonTest whether at least one user is under 20
jq -c '.users | map(select(.name | test("^c")))' u.jsonFilter on a regular expression match against a string field
jq -c '[limit(2; .users[])]' u.jsonTake only the first two results of a generator
jq -c '.users | map({(.name): .age}) | add' u.jsonTurn a list into a name-to-age lookup object
jq -c '.users[0] | with_entries(select(.key != "tags"))' u.jsonDrop one key by filtering the object's key/value pairs
jq -c '.users[0] | pick(.name, .role)' u.jsonKeep only the listed keys, preserving their nesting (jq 1.7+)
jq -c '.users[0] | keys' u.jsonList an object's keys sorted alphabetically
jq '.users[0] | has("email")' u.jsonCheck whether a key exists, even if its value is null
jq -c '.users[] |= (.adult = (.age >= 21))' u.jsonAdd a computed field to every user in place
jq -nc '{"a":1} * {"b":{"c":2}}'Deep-merge two objects; right-hand values win on conflict
jq -c 'del(.users[].tags)' u.jsonDelete the tags field from every user
jq -c --arg r dev '[.users[] | select(.role == $r) | .name]' u.jsonPass a shell value into the filter as a string variable
jq -c --argjson min 25 '[.users[] | select(.age >= $min)]' u.jsonPass a number or JSON value so comparisons work numerically
jq -s 'map(.a) | add' s.jsonSlurp a stream of separate JSON values into one array, then sum a field
jq -n '[1,2,3] | map(. * 10)'Run a filter with null input, no file needed
jq -e '.missing' u.jsonExit with status 1 when the last output is null or false, for use in scripts
jq --tab '.' u.jsonIndent the output with tabs instead of two spaces
jq --slurpfile x s.json '$x' u.jsonLoad every value from a second file into $x as an array
jq --raw-output0 '.users[].name' u.jsonPrint raw strings separated by NUL bytes for xargs -0 (jq 1.7+)
jq -c '[paths(type == "number")]' u.jsonList the path to every number anywhere in the document
jq 'getpath(["users",1,"name"])' u.jsonRead a value from a path given as an array
jq 'setpath(["users",0,"age"]; 35)' u.jsonWrite a value at a path given as an array
jq 'reduce .users[] as $u (0; . + $u.age)' u.jsonSum ages by folding over the users with an accumulator
jq -r '.users[] | [.name, .age] | @csv' u.jsonEmit one CSV row per user with strings quoted
jq -r '.users[] | [.name, .role] | @tsv' u.jsonEmit tab-separated rows for cut, awk or a spreadsheet paste
jq -n '" hi " | trim'Strip leading and trailing whitespace from a string (jq 1.8+)
Worked example: Turn an API response into a CSV of active developers
You have u.json from an internal API and need a CSV of developers aged 25 or more, sorted by age, for a spreadsheet.
MIN=25
jq -r --argjson min "$MIN" '
["name","age","tags"],
(.users
| map(select(.role == "dev" and .age >= $min))
| sort_by(.age)[]
| [.name, .age, (.tags | join(";"))])
| @csv' u.json
Output:
"name","age","tags"
"cy",27,"web;api"
The first array becomes the header row. The second expression filters, sorts and spreads the users back into a stream with [], then builds one array per user; join turns the tags array into one cell, since @csv refuses nested arrays. The comma between the two parts makes jq emit the header and then the rows, and the trailing | @csv applies to both. --argjson matters here: with --arg, $min would be the string "25" and the comparison .age >= $min would compare a number to a string, which in jq's ordering is always false, so you'd get only the header. -r removes the outer JSON quoting so the file opens cleanly. Bo is filtered out for being 19 and Ana for being an admin.
Common mistakes
Passing a number with --arg and getting no matches from a numeric comparison.
Use --argjson for numbers and booleans. --arg always binds a string, and jq sorts every number before every string.
Running map(.name) directly on the top-level object and getting 'Cannot index array with string'.
map works over arrays. Walk to the array first: .users | map(.name), or use .users[].name.
Piping jq output into another command and getting quotes around every value.
Add -r so strings come out raw. Use @csv or @tsv for rows, which also need -r.
Building the filter with shell string interpolation, like "select(.name == \"$NAME\")".
Pass the value with --arg name "$NAME" and use $name in a single-quoted filter. Quotes inside the value can't break it.
Assuming a misspelt key will fail loudly.
jq returns null for missing keys. Use -e in scripts so a null result sets a non-zero exit code, or check with has().
Copying a trim or toboolean example and seeing 'trim/0 is not defined'.
Those arrived in jq 1.8. Check jq --version; macOS still ships 1.7.1, so install a newer build or use sub("^\\s+|\\s+$"; ""; "g").
Related cheat sheets
Sources
- https://jqlang.org/manual/
- https://github.com/jqlang/jq/blob/master/NEWS.md
- https://github.com/jqlang/jq/releases/tag/jq-1.8.2
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.