echo '[{"env":"prod","healthy":true},{"env":"dev","healthy":true}]' | jq '.[] | select(.env == "prod" and .healthy)'Combine predicates with `and` and `or`.
Advanced jq patterns for regex, booleans, dates, math, recursive descent, filtering, and output shaping.
Predicates and truthy tests.
echo '[{"env":"prod","healthy":true},{"env":"dev","healthy":true}]' | jq '.[] | select(.env == "prod" and .healthy)'Combine predicates with `and` and `or`.
echo '[{"role":"admin"},{"role":"editor"}]' | jq '.[] | select(.role == "admin" or .role == "owner")'Great for whitelists and matching multiple states.
echo '[{"active":true},{"active":false}]' | jq '.[] | select(.active | not)'`not` is frequently used inside filters.
echo '"b"' | jq 'IN("a", "b", "c")'Useful for simple membership tests in jq expressions.
Pattern matching and text extraction.
echo '"alice@example.com"' | jq 'test("@example.com$")'Use `test()` for boolean regex checks.
echo '"order-123"' | jq 'match("[0-9]+")'`match()` returns offsets, lengths, and captured text.
echo '"alice@example.com"' | jq 'capture("(?<user>[^@]+)@(?<domain>.+)")'`capture()` is perfect for structured extraction from strings.
echo '"hello world"' | jq 'sub("world"; "jq")'Use `sub()` for one replacement and `gsub()` for all matches.
echo '"a-b-c"' | jq 'gsub("-"; ":")'Very useful for cleanup and normalization.
echo '"a, b; c"' | jq 'splits("[,;] ?")'`splits()` emits values as a stream, which is handy for pipelines.
Math functions and numeric transforms.
echo '3.9' | jq 'floor'Useful for timestamps and bucketing.
echo '3.1' | jq 'ceil'Handy for quota and size calculations.
echo '3.6' | jq 'round'Basic rounding helper.
echo '-42' | jq 'abs'Simple but useful in transformations.
jq -n '[range(0; 5)]'Great for synthetic data and loops.
echo '[3,14,27,39]' | jq 'map((./10 | floor) * 10)'Common analytics-style transformation.
Timestamp parsing and formatting.
jq -n 'now'Useful for generated metadata and quick timestamps.
jq -n 'now | todate'Human-friendly UTC date output.
echo '"2025-01-15T12:34:56Z"' | jq 'fromdateiso8601'Ideal for comparing timestamps numerically.
echo '"2025-01-15 12:34:56"' | jq 'strptime("%Y-%m-%d %H:%M:%S")'Returns a time array, which you can convert with `mktime`.
jq -n 'now | strftime("%Y-%m-%d")'Great for filenames and daily reports.
Recursive descent, encoding, and document-wide transforms.
echo '{"a":1,"b":{"c":2}}' | jq '.. | objects | keys[]'Useful for schema discovery and audits.
echo '{"a":1,"b":[2,{"c":3}]}' | jq '.. | numbers'Recursive descent combines well with type filters like `numbers` and `strings`.
printf 'hello
world
' | jq -Rs '.'Excellent for embedding arbitrary text into JSON payloads.
echo '"<b>hello</b>"' | jq -r '@html'Useful for templating and safe output.
echo '"hello world"' | jq -r '@sh'Great when generating shell commands from JSON data.