OnCallReadyStart learning

jq: command-line JSON processor

jq [-r] [-c] [-n] [-s] [-e] [--arg NAME VALUE] 'FILTER' [file]

Options you will use

-r, --raw-output
Strings come out without quotes - required before you pipe into anything else.
-c, --compact-output
One line per result.
-s, --slurp
Read the whole input into one array.
-n, --null-input
Do not read input; useful with --arg.
-e, --exit-status
Exit 1 if the last output was false or null. For if-tests.
--arg NAME VALUE
Pass a shell value in as $NAME, safely.
.a.b / .[] / .[]?
Field access, array iteration, tolerant iteration.
select(COND) / map(F)
Filter, and apply to every element.
@tsv @csv @json
Format an array as TSV, CSV or a JSON string.
trim / ltrimstr(S) / rtrimstr(S)
Strip surrounding whitespace, or a prefix / suffix. trim, ltrim, rtrim and trimstr are new in jq 1.8; since 1.8 ltrimstr/rtrimstr error on a non-string instead of passing it through.
--version
Print the version: jq-1.8.1 on Ubuntu 26.04.
--argjson NAME JSON
Set $NAME to a JSON value (a number, true, an object) - unlike --arg, which always makes a string.

Examples

$ jq '.items | length' pods.json

how many

$ jq -r '.items[] | select(.status.phase != "Running") | .metadata.name' pods.json

the unhappy pods

$ jq -r '.items[] | [.metadata.namespace, .metadata.name] | @tsv' pods.json

two fields as TSV

$ jq -r '.. | .image? // empty' pods.json | sort -u

every image anywhere in the document

Taught in

On the blog

Try jq in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.