☰
Home
/
Cheatsheets
/
jq Cheatsheet
🔍
jq Cheatsheet
jq filters, transformations, aggregations and CLI flags
☆
Bookmark
Loading tool…
jq Cheatsheet
58 entries · click to copy
🔍 Basic Filters
jq '.'
Pretty-print JSON
jq '.name'
Get field value
jq '.user.email'
Nested field
jq '.items[0]'
First array element
jq '.items[-1]'
Last array element
jq '.items[2:5]'
Array slice (index 2–4)
jq '.items[]'
Iterate all array elements
jq '.[] | .name'
Get name from each item
jq 'keys'
List object keys
jq 'values'
List object values
jq 'length'
Array length or string length
🔄 Transformations
jq '{name: .name, age: .age}'
Build new object
jq '[.[] | .id]'
Map array → extract field
jq '.items | map(.price * .qty)'
Map with expression
jq '.items | map(select(.active))'
Filter array by condition
jq '.items | map(select(.price > 10))'
Filter by numeric comparison
jq '.items | sort_by(.name)'
Sort by field
jq '.items | sort_by(.price) | reverse'
Sort descending
jq '.items | unique_by(.id)'
Deduplicate by field
jq '.items | group_by(.category)'
Group array by field
jq '[.[] | {(.id|tostring): .name}] | add'
Array to object map
∑ Aggregation & Math
jq '[.[] | .price] | add'
Sum all prices
jq '[.[] | .price] | min'
Minimum value
jq '[.[] | .price] | max'
Maximum value
jq '[.[] | .price] | add / length'
Average
jq '.items | length'
Count items
jq '.price * 1.2 | round'
Arithmetic and rounding
jq '.items | map(select(.active)) | length'
Count filtered items
🔤 String Operations
jq '.name | ascii_upcase'
Uppercase
jq '.name | ascii_downcase'
Lowercase
jq '.name | ltrimstr("Mr. ")'
Remove prefix
jq '.name | rtrimstr(" Jr")'
Remove suffix
jq '.name | split(" ")'
Split string into array
jq '.tags | join(", ")'
Join array into string
jq '.name | test("^Al")'
Test regex match (returns bool)
jq '.name | capture("(?<first>\\w+)")'
Extract named capture groups
jq '"Hello \(.name)"'
String interpolation
jq '.id | tostring'
Number to string
jq '.count | tonumber'
String to number
🎯 Conditions & Paths
jq 'if .age > 18 then "adult" else "minor" end'
If/else
jq '.name // "Unknown"'
Default if null or missing
jq '.items? // []'
Optional field, default empty array
jq 'has("email")'
Check if key exists
jq 'in({"a":1})'
Check if value is a key in object
jq 'paths'
All paths in the document
jq 'path(.a.b)'
Path to specific field
jq 'del(.password)'
Remove a field
jq '. + {"role": "admin"}'
Merge / add field
jq '.name |= ascii_upcase'
Update field in-place
💻 CLI Flags
jq -r '.name'
-r / --raw-output: print string without quotes
jq -c '.'
-c / --compact: single-line output
jq -n '{a:1}'
-n / --null-input: no input, construct JSON
jq -e '.ok'
-e: exit 1 if output is false/null
jq --arg name Alice '.greeting = "Hello " + $name'
--arg: pass shell variable as string
jq --argjson n 42 '.count = $n'
--argjson: pass shell variable as JSON value
jq -s '.'
-s / --slurp: read all inputs as array
jq -R '.'
-R / --raw-input: treat input as raw strings
jq -Rs 'split("\n")'
Read file as array of lines