JSON on the command line with jq
Read, filter and reshape JSON in a pipeline with jq: paths, select, map, -r, @csv, --arg, group_by, and the exit codes that tell you a filter found nothing.
Pipes and pipelinesQuoting - single, double and escapesFetching with curl and wget
JSON on the command line with jq
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- grep and awk see lines, but JSON is nested. jq reads structure: .users[0].name walks to a value and .users[] runs the rest of the filter once per element.
- Filters compose with | just like shell pipes. select(cond) keeps matching items, map(f) transforms an array, and {a, b} builds new objects.
- jq prints JSON by default, with strings quoted. Use -r for raw text in shell variables, -c for one object per line, and @csv or @tsv for table output.
- Pass shell values in with --arg name value and refer to $name in the filter. Do not splice variables into the filter text.
- A path that does not exist yields null, not an error. -e makes jq exit 4 when the last output is null or false, and invalid input exits 5, so a script can tell them apart.
Text tools meet structure
curl hands you JSON, and most APIs and many tools print it. The line tools from the streams group are a poor fit. A single JSON value can span many lines or sit on one, a field can move, and quotes and commas live inside strings. A grep '"name"' works until the day the output gets pretty-printed differently.
jq reads the structure. You give it a filter, a small expression that describes a path or a transformation, and it applies it to the JSON on standard input. It is to JSON roughly what awk is to columns.
I ran jq 1.7.1 on macOS (the version Apple ships) against the fixture file data/users.json:
{"users":[{"id":1,"name":"ada","roles":["admin","dev"]},{"id":2,"name":"linus","roles":["dev"]},{"id":3,"name":"grace","roles":[]}]}
Paths
jq . pretty-prints, which is its simplest and most common use, for reading an API response:
$ jq . data/users.json | head -8
{
"users": [
{
"id": 1,
"name": "ada",
"roles": [
"admin",
"dev"
A path walks in. .users[0].name is the name of the first element:
$ jq '.users[0].name' data/users.json
"ada"
[] with no index means every element, and the rest of the filter runs once for each. Note the quotes. That is a JSON string, and for shell use you almost always want -r:
$ jq -r '.users[].name' data/users.json
ada
linus
grace
Quote the filter in single quotes. It is full of characters the shell would otherwise act on, as in the quoting lesson.
Filtering and reshaping with pipes
Inside jq, | feeds the output of one filter into the next, like a shell pipe. select keeps what passes a test:
$ jq -r '.users[] | select(.id > 1) | .name' data/users.json
linus
grace
$ jq -r '.users[] | select(.roles | index("admin")) | .name' data/users.json
ada
You can build new objects. {name, n_roles: (.roles|length)} takes name as is and computes a count, and -c prints each result on one line:
$ jq -c '.users[] | {name, n_roles: (.roles|length)}' data/users.json
{"name":"ada","n_roles":2}
{"name":"linus","n_roles":1}
{"name":"grace","n_roles":0}
map(f) applies a filter across an array, length counts, and group_by(.) sorts into buckets. Counting how many users hold each role:
$ jq -c '[.users[].roles[]] | group_by(.) | map({role: .[0], n: length})' data/users.json
[{"role":"admin","n":1},{"role":"dev","n":2}]
And |= updates a value in place:
$ jq -c '.users[0].name |= ascii_upcase | .users[0]' data/users.json
{"id":1,"name":"ADA","roles":["admin","dev"]}
Output for the shell
JSON-quoted output is wrong for tools that expect rows. @tsv and @csv format an array as a line:
$ jq -r '.users[] | [.id, .name] | @tsv' data/users.json
1 ada
2 linus
3 grace
$ jq -r '.users[] | [.id, .name, (.roles|join("+"))] | @csv' data/users.json
1,"ada","admin+dev"
2,"linus","dev"
3,"grace",""
String interpolation covers the rest: jq -r '.items[] | "\(.n)=\(.v)"' printed a=1 and b=2. Output then goes straight into the cut, sort and wc tools.
Getting shell values in
Do not paste a variable into the filter text. Quoting breaks the moment the value has a quote in it, and a value that came from outside can change what the filter does. Use --arg:
$ jq --arg who linus '.users[] | select(.name == $who) | .id' data/users.json
2
The value arrives as a jq string called $who, whatever it contains. I also confirmed that building the filter with double quotes and \"$N\" works for a harmless value (ada returned 1), but that is the fragile way and is not a habit to keep.
Missing, empty and broken
A path that does not exist gives null, not an error:
$ jq '.users[0].email' data/users.json
null
// supplies a default: jq -r '.users[] | .email // "none"' printed none three times. For scripts, the exit status distinguishes cases:
$ jq -e '.users[] | select(.name=="nobody")' data/users.json; echo "exit=$?"
exit=4
$ echo '{"a":' | jq . ; echo "exit=$?"
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
exit=5
Without -e, an empty result still exits 0. With -e, “no match” becomes 4 and bad input stays 5, so if jq -e ... >/dev/null; then tests for presence. Plain text is not JSON: echo 'plain text' | jq . ended in a parse error rather than echoing it back.
For multiple JSON documents, one per line as many log tools write them, jq -s (slurp) collects them into one array. printf '{"s":200}\n{"s":500}\n{"s":200}\n' | jq -sc 'group_by(.s) | map({status: .[0].s, count: length})' gave [{"status":200,"count":2},{"status":500,"count":1}].
About the video
“jq Tutorial for Beginners: Parse, Filter & Transform JSON Logs” is a 17-minute video from ZeroDave Security, published in late 2026. Its chapters line up closely with this lesson: pretty-printing, extracting fields with -r, arrays and nested fields, pipes inside a filter, iteration, select(), building tsv and csv output, handling missing values, NDJSON and contains(). I could not read a transcript, so I do not know how it handles --arg or the -e exit codes covered above. It is a small, recent channel, so I cannot vouch for its track record, and it is in English. Earthly’s longer tutorial, which has chapters on constructors, sorting and counting, map and select, is linked under deeper.
Where this fits
You can now read the data that APIs return. The last everyday tool is for reaching other machines: ssh, scp and rsync.
Check yourself
You run jq '.users[].name' and the names come back in double quotes, which breaks your shell loop. What fixes it?
Add -r for raw output, which prints strings without the JSON quotes: jq -r '.users[].name'.
How do you pass the shell variable $who into a jq filter safely?
Use jq --arg who "$who" '... select(.name == $who)'. The value arrives as a jq string with no quoting problems.
A filter returns nothing for a missing key and the script continues. How can jq signal the miss in its exit status?
Run it with -e. Exit status 4 means the last output was null or false, or there was no output.
How do you turn each user into a CSV row?
Build an array and pipe it to @csv with -r: jq -r '.users[] | [.id, .name] | @csv'.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.