Pretty-print JSON from stdin
Format JSON read from stdin with default pretty output.
echo '{"name":"alice","age":30}' | jq '.'The identity filter `.` returns the input unchanged, but jq formats it nicely.
Core jq syntax, filters, selectors, arrays, objects, variables, sorting, grouping, formatting, and shell-friendly usage.
Basic invocation and output behavior.
Format JSON read from stdin with default pretty output.
echo '{"name":"alice","age":30}' | jq '.'The identity filter `.` returns the input unchanged, but jq formats it nicely.
jq '.' data.jsonUse jq directly on a file path instead of piping with `cat`.
jq -c '.' data.jsonUseful for logs, NDJSON-like processing, or piping into other tools.
echo '{"name":"alice"}' | jq -r '.name'The `-r` flag is essential when you want shell-friendly plain text.
Create JSON from scratch using null input mode.
jq -n '{app: "cheatsheet", enabled: true}'`-n` starts with no input document, which is ideal for generating JSON.
jq -M '.' data.json`-M` disables color even if your terminal supports it.
jq -S '.' data.jsonHelpful for stable diffs and deterministic output.
Pick values by key, path, and array index.
echo '{"name":"alice","age":30}' | jq '.name'Returns the value of the `name` field.
echo '{"user":{"profile":{"email":"a@example.com"}}}' | jq '.user.profile.email'Dot chaining walks nested objects.
echo '{"user":{}}' | jq '.user.profile?.email'The `?` suppresses errors for missing fields.
echo '[10,20,30]' | jq '.[1]'Array indexing is zero-based.
echo '[10,20,30,40,50]' | jq '.[1:4]'This returns elements at indexes 1 through 3.
echo '["a","b","c"]' | jq '.[]'`.[]` expands the array into multiple outputs.
echo '{"a":1,"b":2}' | jq '.[]'On objects, `.[]` returns the values.
echo '{"name":"alice"}' | jq 'has("name")'Use `has()` for existence checks before dereferencing fields.
echo '{"name":"alice","age":30}' | jq 'keys'Useful for introspection and debugging unknown schemas.
echo '{"user":{"name":"alice","roles":["admin"]}}' | jq 'paths'`paths` helps inspect complex or unfamiliar nested JSON structures.
Build, reshape, merge, and inspect collections.
echo '[1,2,3,4]' | jq 'length'`length` works on arrays, strings, and objects.
Transform an array of objects into one field per item.
echo '[{"name":"a"},{"name":"b"}]' | jq 'map(.name)'`map()` applies a filter to each element and returns an array.
echo '[1,2,3,4]' | jq 'add'`add` sums numeric arrays and can also merge arrays or strings depending on input.
echo '[[1,2],[3,[4]]]' | jq 'flatten'`flatten` recursively flattens nested arrays into one array.
echo '[3,1,2,3,2]' | jq 'unique'`unique` sorts and deduplicates.
echo '[3,1,2]' | jq 'sort'Use `sort_by()` for arrays of objects.
echo '[{"name":"b","age":20},{"name":"a","age":30}]' | jq 'sort_by(.name)'`sort_by(.field)` is common for reporting and reformatting API output.
echo '[{"team":"a","name":"x"},{"team":"a","name":"y"},{"team":"b","name":"z"}]' | jq 'group_by(.team)'Input should already be sorted by the grouping field for predictable results.
jq -n '{name:"alice",age:30} + {age:31,city:"SF"}'The `+` operator merges objects shallowly.
echo '{"id":1,"name":"alice","email":"a@example.com"}' | jq '{id, name}'Object construction is central to jq transformations.
Select, filter, update, and conditionally transform JSON.
Filter array items based on a boolean expression.
echo '[{"name":"a","age":20},{"name":"b","age":35}]' | jq '.[] | select(.age >= 30)'`select()` keeps only inputs that satisfy the condition.
echo '[{"name":"a","active":true},{"name":"b","active":false}]' | jq 'map(select(.active))'Wrap `select()` with `map()` when you want an array back.
echo '{"name":"alice"}' | jq '.email // "unknown@example.com"'The `//` operator provides a default value.
echo '{"score":85}' | jq 'if .score >= 80 then "pass" else "fail" end'jq supports full conditional branching with `if ... then ... else ... end`.
echo '{"name":"alice","age":30}' | jq '.age = 31'Assignment lets you modify existing JSON structures.
echo '{"count":5}' | jq '.count += 1'Arithmetic update operators work on matching scalar types.
echo '{"name":"alice","password":"secret"}' | jq 'del(.password)'`del()` is useful for sanitizing secrets or noisy fields.
echo '{"user":{"profile":{"ssn":"123","name":"alice"}}}' | jq 'del(.user.profile.ssn)'You can target nested paths directly inside `del()`.
echo '[1,null,2,null]' | jq '.[] | select(. != null)'Use `empty` or `select()` to suppress unwanted outputs.
Use variables, accumulators, and custom reshaping patterns.
echo '{"user":{"name":"alice"},"team":"eng"}' | jq '.user as $u | {name: $u.name, team: .team}'The `as $var` syntax avoids repeating long paths.
echo '[1,2,3,4]' | jq 'reduce .[] as $n (0; . + $n)'`reduce` is powerful for custom aggregations.
echo '[{"id":1,"name":"a"},{"id":2,"name":"b"}]' | jq 'reduce .[] as $item ({}; .[$item.id|tostring] = $item.name)'This pattern is common when converting lists into keyed lookup tables.
echo '{"a":1,"b":2}' | jq 'with_entries(.key |= ascii_upcase)'`with_entries()` is object-oriented `map()` over `to_entries` output.
Represent an object as key-value entry objects.
echo '{"a":1,"b":2}' | jq 'to_entries'Useful when you need to filter or sort by object keys or values.
Turn key-value entry objects into a normal object.
echo '[{"key":"a","value":1},{"key":"b","value":2}]' | jq 'from_entries'Often paired with `to_entries | map(...) | from_entries`.
Walk the document and mutate matching values.
echo '{"name":"alice","nested":["dev","ops"]}' | jq 'walk(if type == "string" then ascii_upcase else . end)'`walk()` is very useful for recursive normalization when available in your jq version.
Join, split, encode, decode, and shape text output.
echo '["alice","bob","carol"]' | jq -r 'join(", ")'Great for turning arrays into CSV-like text.
echo 'alice,bob,carol' | jq 'split(",")'jq can move fluidly between strings and arrays.
echo '{"name":"alice","age":30}' | jq -r '"(.name) is (.age) years old"'Interpolation is one of jq's most useful shell-reporting features.
echo '["alice",30,"SF"]' | jq -r '@csv'Use `@csv` for CSV-safe quoting and escaping.
echo '["alice",30,"SF"]' | jq -r '@tsv'`@tsv` is useful for shell tables and spreadsheet imports.
echo 'hello world@example.com' | jq -sRr @uri`-sRr` slurps raw text and returns an encoded plain string.
echo 'hello world' | jq -sRr @base64Useful for quick payload generation in scripts.
echo '"aGVsbG8gd29ybGQ="' | jq -r '@base64d'Input must be a JSON string containing base64 data.
Read multiple inputs, raw lines, and streaming data.
jq -s '.' file1.json file2.json`-s` slurps all input documents into one array before applying the filter.
printf 'alpha
beta
' | jq -R '.'`-R` makes jq treat each line as a raw string.
cat notes.txt | jq -Rs '.'`-Rs` is useful when you need to embed or encode an entire file.
Turn multiple JSON documents into a single array.
printf '{"id":1}
{"id":2}
' | jq -s '.'Great for NDJSON aggregation and batch transforms.
Process large JSON incrementally as path/value events.
jq --stream '.' large.jsonStreaming mode is advanced but valuable for huge documents.
jq '{file: input_filename, value: .}' data.jsonUseful when processing many input files together.