jq Filters and Transformations

Advanced jq patterns for regex, booleans, dates, math, recursive descent, filtering, and output shaping.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Boolean Logic
Logical AND
echo '[{"env":"prod","healthy":true},{"env":"dev","healthy":true}]' | jq '.[] | select(.env == "prod" and .healthy)'

# Filter items that satisfy two conditions.

Logical OR
echo '[{"role":"admin"},{"role":"editor"}]' | jq '.[] | select(.role == "admin" or .role == "owner")'

# Match either of two conditions.

Logical NOT
echo '[{"active":true},{"active":false}]' | jq '.[] | select(.active | not)'

# Invert a predicate.

Test membership with IN
echo '"b"' | jq 'IN("a", "b", "c")'

# Check whether a value appears in a stream.

## Regex and Text Processing
Test regex match
echo '"alice@example.com"' | jq 'test("@example.com$")'

# Return true if a string matches a pattern.

Get regex match details
echo '"order-123"' | jq 'match("[0-9]+")'

# Return structured regex match metadata.

Capture named regex groups
echo '"alice@example.com"' | jq 'capture("(?<user>[^@]+)@(?<domain>.+)")'

# Extract named groups into an object.

Replace first regex match
echo '"hello world"' | jq 'sub("world"; "jq")'

# Replace text using a regex pattern.

Replace all matches
echo '"a-b-c"' | jq 'gsub("-"; ":")'

# Replace every instance of a pattern.

Split with regex
echo '"a, b; c"' | jq 'splits("[,;] ?")'

# Split a string using a regex separator.

## Numbers and Math
Floor number
echo '3.9' | jq 'floor'

# Round down to nearest integer.

Ceiling number
echo '3.1' | jq 'ceil'

# Round up to nearest integer.

Round number
echo '3.6' | jq 'round'

# Round to the nearest integer.

Absolute value
echo '-42' | jq 'abs'

# Return the absolute value of a number.

Generate numeric range
jq -n '[range(0; 5)]'

# Create a sequence of integers.

Bucket numbers
echo '[3,14,27,39]' | jq 'map((./10 | floor) * 10)'

# Convert numbers into 10-point buckets.

## Dates and Time
Current Unix time
jq -n 'now'

# Get the current time as Unix epoch seconds.

Unix time to ISO string
jq -n 'now | todate'

# Format epoch seconds as an ISO-like UTC string.

ISO string to Unix time
echo '"2025-01-15T12:34:56Z"' | jq 'fromdateiso8601'

# Parse an ISO timestamp into epoch seconds.

Parse custom date format
echo '"2025-01-15 12:34:56"' | jq 'strptime("%Y-%m-%d %H:%M:%S")'

# Parse a custom-formatted date string.

Format epoch as custom string
jq -n 'now | strftime("%Y-%m-%d")'

# Render timestamps using a custom format.

## Encoding and Recursion
Find all keys recursively
echo '{"a":1,"b":{"c":2}}' | jq '.. | objects | keys[]'

# Emit every key in nested objects.

Find all numbers recursively
echo '{"a":1,"b":[2,{"c":3}]}' | jq '.. | numbers'

# Emit every number in a document.

JSON-encode raw text
printf 'hello
world
' | jq -Rs '.'

# Turn raw text into a valid JSON string.

HTML-escape a string
echo '"<b>hello</b>"' | jq -r '@html'

# Escape special HTML characters.

Shell-escape a string
echo '"hello world"' | jq -r '@sh'

# Produce a shell-safe quoted string.

Recommended next

No recommendations yet.