jq Cheat Sheet: 38 Essential Commands with Examples
jq is a small command-line program for reading, filtering and reshaping JSON. It is installed on most CI images and can be added to any machine in seconds. This cheat sheet groups the commands you will use most, and every example below was run with jq 1.7 on the sample file shown first, so the outputs are real.
Install
brew install jq # macOS
sudo apt install jq # Debian / Ubuntu
winget install jqlang.jq # WindowsThe sample file (users.json)
{
"users": [
{ "id": 1, "name": "Ann", "email": "ann@example.com", "age": 34, "active": true,
"roles": ["admin", "dev"], "address": { "city": "Berlin" } },
{ "id": 2, "name": "Bob", "email": "bob@example.com", "age": 27, "active": false,
"roles": ["dev"], "address": { "city": "Cairo" } },
{ "id": 3, "name": "Cleo", "email": null, "age": 41, "active": true,
"roles": [], "address": { "city": "Berlin" } }
]
}1. Read values
$ jq '.users[0].name' users.json
"Ann"
$ jq -r '.users[0].name' users.json # -r = raw output, no quotes
Ann
$ jq '.users | length' users.json
3
$ jq -r '.users[].name' users.json # [] = every element
Ann
Bob
Cleo
$ jq -c '.users[1:] | map(.id)' users.json # slices work like Python
[2,3]-c prints each result on one line (compact). Without it, jq pretty-prints. Use -r whenever the output goes into another shell command, otherwise you get the quotes too.
2. Pick fields
$ jq -c '.users[] | {id, name}' users.json
{"id":1,"name":"Ann"}
{"id":2,"name":"Bob"}
{"id":3,"name":"Cleo"}
$ jq -c '.users[0] | keys' users.json # keys are sorted; keys_unsorted keeps file order
["active","address","age","email","id","name","roles"]3. Filter with select
$ jq -c '[.users[] | select(.active)] | map(.name)' users.json
["Ann","Cleo"]
$ jq -c '.users | map(select(.age > 30)) | map(.name)' users.json
["Ann","Cleo"]
$ jq -c '.users[] | select(.roles | index("admin")) | .name' users.json
"Ann"
$ jq -c '.users[] | select(.email == null) | .id' users.json
3.users[] | select(...) outputs matching items one by one. Wrap the whole thing in [ ] (or use map(select(...))) when you want a single array back.
4. Use shell variables safely
$ jq --arg city Berlin -c '[.users[] | select(.address.city == $city) | .name]' users.json
["Ann","Cleo"]"select(.city == \"$CITY\")"). Quotes in the value break the filter. Use --arg for strings and --argjson for numbers, booleans or JSON.5. Sort, group, count
$ jq -c '.users | sort_by(.age) | map(.name)' users.json
["Bob","Ann","Cleo"]
$ jq -c '.users | sort_by(-.age) | map(.name)' users.json # descending
["Cleo","Ann","Bob"]
$ jq -c '.users | map(.address.city) | unique' users.json
["Berlin","Cairo"]
$ jq -c '.users | group_by(.address.city) | map({city: .[0].address.city, count: length})' users.json
[{"city":"Berlin","count":2},{"city":"Cairo","count":1}]
$ jq '.users | map(.age) | add / length' users.json # average
34
$ jq -r '.users | max_by(.age) | .name' users.json
Cleo
$ jq -c '.users | (map(select(.active)) | length) as $n | {active: $n, total: length}' users.json
{"active":2,"total":3}6. Change data
$ jq -c '.users[0] | del(.email, .roles)' users.json
{"id":1,"name":"Ann","age":34,"active":true,"address":{"city":"Berlin"}}
$ jq -c '.users[0] | .country = "DE" | {name, country}' users.json
{"name":"Ann","country":"DE"}
$ jq '.users[0] | .age += 1 | .age' users.json
35
$ jq -c '.users | map(.email // "none")' users.json # // = default for null/false
["ann@example.com","bob@example.com","none"]
$ jq -c '.users[0] | with_entries(.key |= ascii_upcase) | keys' users.json
["ACTIVE","ADDRESS","AGE","EMAIL","ID","NAME","ROLES"]
$ jq -c '.users[0] | to_entries | map(select(.value | type == "number")) | from_entries' users.json
{"id":1,"age":34}jq never edits the file in place. To save the result, write to a new file and move it: jq '…' in.json > tmp.json && mv tmp.json in.json. Writing straight back to in.json with > empties the file before jq reads it.
7. Reshape
$ jq -c '.users | map({(.name): .age}) | add' users.json # array → object
{"Ann":34,"Bob":27,"Cleo":41}
$ jq -c '.users | INDEX(.id) | keys' users.json # index objects by a field
["1","2","3"]
$ jq -c '.users | map(.roles) | flatten | unique' users.json
["admin","dev"]
$ jq -c '.. | .city? // empty' users.json # find a key at any depth
"Berlin"
"Cairo"
"Berlin"8. Export to CSV, TSV and text
$ jq -r '.users[] | [.id, .name, .address.city] | @csv' users.json
1,"Ann","Berlin"
2,"Bob","Cairo"
3,"Cleo","Berlin"
$ jq -r '.users[] | [.id, .name] | @tsv' users.json
1 Ann
2 Bob
3 Cleo
$ jq -r '.users[] | "\(.name) lives in \(.address.city)"' users.json
Ann lives in Berlin
Bob lives in Cairo
Cleo lives in Berlin@csv quotes strings and escapes quotes correctly, which join(",") does not. For deeply nested data with many columns, the JSON to CSV converter flattens everything automatically.
9. Multiple files and JSON Lines
$ jq -c -s 'add' a.json b.json # merge {"a":1} and {"b":2}
{"a":1,"b":2}
$ jq -c '.users[]' users.json > users.ndjson # array → one object per line
$ jq -c -s 'map(.id)' users.ndjson # -s (slurp) reads all lines into one array
[1,2,3]10. Explore unknown JSON
$ jq -c '[paths | map(tostring) | join(".")] | .[0:4]' users.json
["users","users.0","users.0.id","users.0.name"]
$ jq -c '[paths(type == "number")] | length' users.json
611. Use jq in scripts
$ jq -e '.users[0].missing' users.json; echo "exit code: $?"
null
exit code: 1
$ echo '{"a":1,}' | jq . ; echo "exit code: $?"
jq: parse error: Expected another key-value pair at line 1, column 8
exit code: 5-e makes jq exit with code 1 when the result is null or false, which is handy in CI checks. Invalid JSON gives exit code 5 and a short message. For a clearer explanation of what’s wrong, paste the JSON into the JSON Validator, or let JSON Repair fix trailing commas, comments and quotes automatically.
jq vs JSONPath
jq is a full language: it filters, transforms and outputs new JSON. JSONPath ($.users[?(@.age > 30)].name) only selects values, but it is built into many tools, databases and API test suites. If you mostly need to find values, see the JSONPath tutorial.