jq Cheat Sheet: Filters, Selectors & Transformations
The jq filters and selectors for slicing JSON in one quick reference.
Select, filter, and reshape JSON on the command line with jq.
Selectors
| Filter | Does |
|---|---|
| .key | Field value |
| .a.b.c | Nested field |
| .[] | Iterate array/object values |
| .[0] | First element |
| .key // "x" | Default if null |
| .["weird-key"] | Bracket access |
Transform
| Filter | Does |
|---|---|
| map(.name) | Project a field |
| select(.age > 18) | Keep matching |
| {name, id} | Reshape object |
| group_by(.type) | Group |
| sort_by(.ts) | Sort |
| length | Count / string length |
| keys | Object keys |
Flags & examples
Terminal
jq -r '.items[].name' data.json # -r: raw strings
jq '.[] | select(.active)' data.json
jq -n --arg v "$VAL" '{value: $v}' # build from shell var
curl -s api/x | jq '.data | length'Key takeaways
- -r outputs raw strings (no quotes) for shell consumption.
- select() filters; map() projects across an array.
- --arg safely injects a shell variable into a jq program.
Related guides
sed & awk Cheat Sheet: Text Processing One-LinersA sed and awk cheat sheet - substitution, deletion, field extraction, and the one-liners that power text proc…
curl Cheat Sheet: Methods, Headers, Auth & DownloadsA curl cheat sheet - HTTP methods, headers, JSON bodies, auth, downloads, and the flags you use for API calls…
Bash Scripting Cheat Sheet: Variables, Tests & LoopsA bash scripting cheat sheet - variables, parameter expansion, test conditions, loops, functions, and strict-…