jq Commands

jq commands cheatsheet.

Filter, select, transform and aggregate JSON right in the terminal — the jq filters you reach for every day. Tap to copy.

Basics
jq '.'Pretty-print the input JSON
jq -r '.name'Output a raw string without quotes
jq '.foo.bar'Access a nested field
jq '.foo?'Access a field without erroring if missing
echo '{"a":1}' | jq '.a'Pipe JSON into jq
Arrays
jq '.[]'Iterate over array elements
jq '.[0]'Get the first element by index
jq '.[2:4]'Slice a range of elements
jq 'length'Count elements (or string length)
jq 'map(.price)'Transform each element into a new array
Select & filter
jq 'select(.age > 30)'Keep inputs matching a condition
jq '.items[] | select(.active)'Filter array elements by a field
jq 'has("key")'Test whether an object has a key
jq 'map(select(.qty > 0))'Filter an array in place
jq '.[] | select(.name == "Alice")'Match a field value
Transform
jq '{name, id}'Construct an object from fields
jq '{n: .name}'Build an object with renamed keys
jq '.name = "new"'Set a field value
jq 'to_entries'Convert an object to key/value pairs
jq 'keys'List an object's keys, sorted
jq '.a as $x | .b + $x'Bind a value to a variable
Aggregate
jq 'add'Sum numbers (or concatenate arrays)
jq 'group_by(.type)'Group array items by a field
jq 'sort_by(.date)'Sort an array by a field
jq 'unique'Remove duplicate values
jq 'min'Smallest value in an array
jq 'max'Largest value in an array
Options / invocation
jq -c '.'Compact output (one line)
jq -s '.'Slurp all inputs into one array
jq -n 'now'Run without input (null input)
jq --arg name val '.x = $name'Pass a string variable
jq -e '.active'Set the exit status from the output
cat file.json | jq '.'Pipe a file into jq

What jq actually does

jq is a tiny command-line tool that treats JSON as a first-class data type, letting you slice, reshape and summarise it without writing a script. You hand it some JSON on standard input, give it a filter — a little expression describing what you want out — and it streams the result back. The simplest filter is ., the identity filter, which just pretty-prints whatever it reads, and almost everything else builds on that same dot-path idea.

Paths, pipes and raw output

You navigate into data with dot paths: .name pulls a field, .user.email reaches a nested one, and .[] spreads an array into a stream of its elements. Filters chain together with the pipe |, exactly like a shell pipeline, so .items[] | .price reads as "for each item, take its price". By default jq prints values as JSON, which means strings arrive wrapped in quotes; add -r when you want a bare string you can feed into another command.

Selecting and transforming

The select function is how you filter: .[] | select(.active) keeps only the elements that pass a test, and comparisons like select(.age > 30) work as you'd expect. To reshape data you build new objects with {}{name, id} plucks two fields, while {n: .name} renames one — and map() applies a transform across a whole array. Helpers like keys, to_entries and has() round out most day-to-day object work.

Summaries and invocation flags

For aggregation, add sums an array, group_by and sort_by organise records by a field, and unique dedupes. A handful of flags change how jq runs: -c compacts each result onto one line, -s slurps multiple inputs into a single array, -n runs with null input, and --arg injects a shell value as a jq variable. jq pairs naturally with the curl commands for fetching an API response and the grep commands when you need plain-text search alongside it.

FAQ

What's the difference between jq -r and the default output?
By default jq prints JSON-encoded values, so string results come out wrapped in double quotes. -r (--raw-output) writes strings as plain text without the quotes, which is what you want when piping a value into other shell commands.
How do I filter an array by a field value?
Iterate the array and pipe into select. For example jq '.items[] | select(.active)' keeps only elements where .active is truthy, and jq 'select(.age > 30)' keeps ones matching a numeric comparison.
How do I print jq output as one line per record?
Use -c (--compact-output), for example jq -c '.[]', which prints each value on a single line instead of pretty-printed across several — handy for producing JSONL or feeding another tool.

More cheatsheets