cheat-sheets

jq

jq is a command-line JSON processor. Think of it as sed or awk for JSON (SED Cheat-Sheet, AWK). For YAML see yq.

Documentation: jq Manual

Options #

OPTIONDESCRIPTION
-rRaw output, strip quotes from strings
-cCompact one-line output
-eExit non-zero if the last output is false or null
-sSlurp all inputs into one array
-nNo input, build from scratch
--arg <NAME> <VALUE>Pass a string variable ($NAME)
--argjson <NAME> '<JSON>'Pass a JSON variable
-SSort object keys
--tabIndent with tabs

Selecting #

FILTERDESCRIPTION
.Pretty-print the input
.nameField
.a.b.cNested field
.["key-with-dash"]Awkward key names
.items[0]First element
.items[-1]Last element
.items[]Every element
.items[2:5]Slice
.name?Suppress errors if missing
.a // "default"Default when null or false
keysObject keys
lengthLength of array, object or string
.. | .id? // emptyRecursively find every id

Filtering and Transforming #

FILTERDESCRIPTION
.items[] | select(.status == "ok")Keep matching elements
.items[] | select(.name | test("^web"))Regex match
.items[] | select(.tags | index("prod"))Array contains
map(.id)Transform an array
map(select(.age > 30))Filter an array
[.items[] | {name, id}]Build smaller objects
{name: .title, tags: (.labels | join(","))}Reshape
sort_by(.date)Sort
group_by(.type) | map({type: .[0].type, n: length})Group and count
unique_by(.id)Deduplicate
addSum numbers or concatenate
to_entries, from_entries, with_entries(...)Convert objects to key/value lists and back
del(.password)Remove a key
.a = 5 / .a |= . + 1Set or update
first, last, min_by(.x), max_by(.x)Pick one

Output Formats #

COMMANDDESCRIPTION
jq -r '.items[] | [.name, .id] | @csv'CSV
jq -r '.items[] | [.name, .id] | @tsv'TSV
jq -r '.name | @sh'Shell-safe quoting
jq -r '.data | @base64' / @base64dBase64 encode / decode
jq -r '.q | @uri'URL-encode
jq -r '"Name: \(.name)"'String interpolation

Everyday Examples #

COMMANDDESCRIPTION
curl -s <URL> | jq .Pretty-print an API response
docker inspect <C> | jq -r '.[0].NetworkSettings.IPAddress'Container IP
kubectl get pods -o json | jq -r '.items[] | select(.status.phase!="Running") | .metadata.name'Pods not running
jq -s 'add' a.json b.jsonMerge arrays from files
jq -s '.[0] * .[1]' a.json b.jsonDeep merge two objects
jq --arg v "1.2.3" '.version = $v' package.jsonSet a value from a shell variable
jq 'paths(scalars) as $p | "\($p | join(".")) = \(getpath($p))"' -r f.jsonFlatten to dotted paths
jq -n '[inputs]' file.ndjsonNDJSON to an array
jq -e '.ok == true' resp.json >/dev/nullUse as a test in scripts
Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.