jq Cheat Sheet

Core jq syntax, filters, selectors, arrays, objects, variables, sorting, grouping, formatting, and shell-friendly usage.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Getting Started
Pretty-print JSON from stdin
echo '{"name":"alice","age":30}' | jq '.'

# Format JSON read from stdin with default pretty output.

Read JSON file
jq '.' data.json

# Process a local JSON file.

Compact output
jq -c '.' data.json

# Emit compact JSON on one line per result.

Raw string output
echo '{"name":"alice"}' | jq -r '.name'

# Print strings without JSON quotes.

Run jq without input
jq -n '{app: "cheatsheet", enabled: true}'

# Create JSON from scratch using null input mode.

Disable color output
jq -M '.' data.json

# Print monochrome JSON for scripts or files.

Sort object keys
jq -S '.' data.json

# Output JSON with keys in sorted order.

## Selectors and Paths
Get object property
echo '{"name":"alice","age":30}' | jq '.name'

# Select a top-level property by key.

Get nested property
echo '{"user":{"profile":{"email":"a@example.com"}}}' | jq '.user.profile.email'

# Select a value from a nested path.

Optional selector
echo '{"user":{}}' | jq '.user.profile?.email'

# Avoid errors when a key may be missing.

Get array element by index
echo '[10,20,30]' | jq '.[1]'

# Return an item from an array by position.

Slice an array
echo '[10,20,30,40,50]' | jq '.[1:4]'

# Return a subrange of elements.

Iterate over array items
echo '["a","b","c"]' | jq '.[]'

# Emit each array element separately.

Iterate object values
echo '{"a":1,"b":2}' | jq '.[]'

# Emit each value in an object.

Check whether key exists
echo '{"name":"alice"}' | jq 'has("name")'

# Return true if an object has a key.

List object keys
echo '{"name":"alice","age":30}' | jq 'keys'

# Return the keys of an object as an array.

List all JSON paths
echo '{"user":{"name":"alice","roles":["admin"]}}' | jq 'paths'

# Emit all existing paths inside a document.

## Arrays and Objects
Array length
echo '[1,2,3,4]' | jq 'length'

# Return the number of items in an array.

Map each item to a property
echo '[{"name":"a"},{"name":"b"}]' | jq 'map(.name)'

# Transform an array of objects into one field per item.

Sum array values
echo '[1,2,3,4]' | jq 'add'

# Add all numbers in an array.

Flatten nested arrays
echo '[[1,2],[3,[4]]]' | jq 'flatten'

# Collapse one level or all levels of array nesting.

Unique values
echo '[3,1,2,3,2]' | jq 'unique'

# Remove duplicate items from an array.

Sort an array
echo '[3,1,2]' | jq 'sort'

# Sort scalar array values.

Sort array of objects by field
echo '[{"name":"b","age":20},{"name":"a","age":30}]' | jq 'sort_by(.name)'

# Order objects by a chosen property.

Group array items by field
echo '[{"team":"a","name":"x"},{"team":"a","name":"y"},{"team":"b","name":"z"}]' | jq 'group_by(.team)'

# Group objects into buckets by a property.

Merge objects
jq -n '{name:"alice",age:30} + {age:31,city:"SF"}'

# Combine two objects with later keys overwriting earlier ones.

Build new object
echo '{"id":1,"name":"alice","email":"a@example.com"}' | jq '{id, name}'

# Construct an object from selected fields.

## Filters and Conditions
Select objects matching condition
echo '[{"name":"a","age":20},{"name":"b","age":35}]' | jq '.[] | select(.age >= 30)'

# Filter array items based on a boolean expression.

Filter array and keep array output
echo '[{"name":"a","active":true},{"name":"b","active":false}]' | jq 'map(select(.active))'

# Return matching elements as an array.

Fallback default value
echo '{"name":"alice"}' | jq '.email // "unknown@example.com"'

# Use a fallback when a value is null or missing.

Conditional expression
echo '{"score":85}' | jq 'if .score >= 80 then "pass" else "fail" end'

# Branch based on input values.

Update field value
echo '{"name":"alice","age":30}' | jq '.age = 31'

# Set an object field to a new value.

Increment numeric field
echo '{"count":5}' | jq '.count += 1'

# Increase a numeric value in place.

Delete object key
echo '{"name":"alice","password":"secret"}' | jq 'del(.password)'

# Remove a field from an object.

Delete nested key
echo '{"user":{"profile":{"ssn":"123","name":"alice"}}}' | jq 'del(.user.profile.ssn)'

# Remove a deeply nested field.

Drop null values from stream
echo '[1,null,2,null]' | jq '.[] | select(. != null)'

# Suppress null outputs entirely.

## Variables, Reduce, and Advanced Transformations
Capture value into variable
echo '{"user":{"name":"alice"},"team":"eng"}' | jq '.user as $u | {name: $u.name, team: .team}'

# Store a subexpression and reuse it later.

Reduce array to sum
echo '[1,2,3,4]' | jq 'reduce .[] as $n (0; . + $n)'

# Aggregate values manually with `reduce`.

Reduce array into object map
echo '[{"id":1,"name":"a"},{"id":2,"name":"b"}]' | jq 'reduce .[] as $item ({}; .[$item.id|tostring] = $item.name)'

# Build an object keyed by a field.

Transform object entries
echo '{"a":1,"b":2}' | jq 'with_entries(.key |= ascii_upcase)'

# Update every key-value pair in an object.

Convert object to entries array
echo '{"a":1,"b":2}' | jq 'to_entries'

# Represent an object as key-value entry objects.

Convert entries back to object
echo '[{"key":"a","value":1},{"key":"b","value":2}]' | jq 'from_entries'

# Turn key-value entry objects into a normal object.

Recursively transform all strings
echo '{"name":"alice","nested":["dev","ops"]}' | jq 'walk(if type == "string" then ascii_upcase else . end)'

# Walk the document and mutate matching values.

## Strings and Formatting
Join array into string
echo '["alice","bob","carol"]' | jq -r 'join(", ")'

# Join array items with a delimiter.

Split string into array
echo 'alice,bob,carol' | jq 'split(",")'

# Break a string apart using a delimiter.

String interpolation
echo '{"name":"alice","age":30}' | jq -r '"(.name) is (.age) years old"'

# Build human-readable text from JSON values.

Convert array to CSV row
echo '["alice",30,"SF"]' | jq -r '@csv'

# Output a JSON array as CSV.

Convert array to TSV row
echo '["alice",30,"SF"]' | jq -r '@tsv'

# Output a JSON array as tab-separated text.

URL-encode a string
echo 'hello world@example.com' | jq -sRr @uri

# Encode a string for use inside URLs or query parameters.

Base64-encode string
echo 'hello world' | jq -sRr @base64

# Encode a string as base64.

Base64-decode string
echo '"aGVsbG8gd29ybGQ="' | jq -r '@base64d'

# Decode a base64 string back to plain text.

## Files, Slurp, and Streams
Slurp inputs into one array
jq -s '.' file1.json file2.json

# Read all JSON inputs and wrap them into an array.

Read raw lines as strings
printf 'alpha
beta
' | jq -R '.'

# Treat input as plain text lines instead of JSON.

Read whole raw file as one string
cat notes.txt | jq -Rs '.'

# Slurp plain text into one string value.

Convert JSON lines into array
printf '{"id":1}
{"id":2}
' | jq -s '.'

# Turn multiple JSON documents into a single array.

Use streaming parser
jq --stream '.' large.json

# Process large JSON incrementally as path/value events.

Include filename in results
jq '{file: input_filename, value: .}' data.json

# Annotate output with the source file name.

Recommended next

No recommendations yet.