Blog post: jq recipes you’ll actually use Reference: jq manual
Problem
JSON from an API/CLI/config file needs reshaping — extract fields, filter elements, flatten to lines — without writing a script. jq’s filter language does it in one expression; these are the patterns that cover ~95% of daily use.
Basics
jq '.' # pretty-print
jq '.field' # one field
jq '.a.b.c' # nested field
jq '.items[0]' # first array element
jq '.items[-1]' # last element
jq '.items[0:3]' # slice (first 3)
jq 'length' # array/object size
jq 'keys' # object keys
Arrays
jq '.[]' # every element (flatten)
jq '.[].name' # one field from each
jq '.[].name' -r # -r: raw output, no quotes
jq 'map(.name)' # same, but stays an array
jq 'map(.name) | sort' # sort the array
jq 'map(.name) | unique' # dedupe
jq 'first(.[] | select(.active))' # first match
jq 'contains(["a","b"])' # array includes all listed
jq '.[] | select(.x > 5)' # filter elements
jq 'group_by(.env)' # group into arrays
Objects
jq '{name, id}' # build object from fields
jq '.[] | {name, id}' # object per element
jq 'to_entries' # [{key, value}, ...]
jq 'to_entries[] | "\(.key)=\(.value)"' # flatten to key=value
jq 'with_entries(.key |= ascii_upcase)' # transform all keys
jq 'del(.secrets)' # remove fields
jq '.a + .b' # merge objects
Strings & interpolation
jq '.[] | "\(.org)/\(.repo):\(.version)"' # interpolation
jq -r '.[] | join(",")' # join array fields
jq 'test("regex")' # boolean match
jq '.[] | select(.name | test("^harbour-"))' # filter by regex
jq 'ascii_downcase' # case ops
jq 'ltrimstr("prefix")' # strip prefix
Conditionals & logic
jq 'if .env == "prod" then "p" else "np" end'
jq '.[] | select(.env == "prod" and .enabled)'
jq '.value // "default"' # fallback for null/missing
jq '.items | any(.ready)' # any element true
jq '.items | all(.ready)' # all elements true
jq '.items | INDEX(.id)' # array → map keyed by .id
Aggregation
jq '[.[] | .size] | add' # sum
jq '[.[] | .v] | min, max' # min/max
jq 'sort_by(.created) | last' # latest
jq 'map(select(.x)) | length' # count matches
Handy combos
jq -r '.[] | [.id, .name] | @tsv' # tab-separated rows
jq -r '.[] | [.id, .name] | @csv' # CSV
jq -r 'paths | join(".")' # every leaf path
jq 'getpath(["a","b"])' # safe nested get
jq '.. | objects | select(has("token"))' # find anywhere in tree
jq -S '.' # sort all keys (canonical diff)
gh api patterns
# one field per element
gh api orgs/ORG/installations --jq '.installations[] | {app_id, app_slug}'
# find a specific app
gh api orgs/ORG/installations \
--jq '.installations[] | select(.app_slug=="ifc-mentor-jenkins") | {app_id, id}'
# file list from a tree
gh api repos/ORG/REPO/git/trees/main?recursive=1 --jq '.tree[].path'
# repo names only
gh api 'orgs/ORG/repos?per_page=100' --jq '.[].name'
# count
gh api 'orgs/ORG/repos?per_page=100' --jq 'length'
# raw file content (skip jq entirely)
gh api repos/ORG/REPO/contents/path.yaml -H "Accept: application/vnd.github.raw"
Gotchas
.foo.barfails on null parents → use.foo?.baror.foo // {}.-rfor scripts/config; keep JSON mode when piping to another jq.select()filters elements;map()transforms — mixing them up is the #1 mistake.@tsv/@csvneed-rto look right.gh api --jq=gh apithen pipe to jq; identical togh api ... | jq ....