Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

jq

Read, filter and change JSON on the command line: API answers, kubectl -o json, cloud CLI output.

Level
DevOps tool
Group
Text and search
Needs root
No
Package
jq

What it does

jq is a small language for JSON. It reads JSON from a file or a pipe, applies a filter and prints the result. . is the whole input, .name a field, .[] every item of an array, | passes the result to the next filter.

DevOps work is full of JSON: REST APIs (curl), kubectl get -o json, aws, az, gh api, Terraform output. jq turns that into the one value a script needs.

How to write it

jq [OPTIONS] FILTER [FILE ...]

Options 21

Most used

Output

-r, --raw-output
Print strings without quotes (for shell variables).
-c, --compact-output
One JSON value per line.
-j, --join-output
Like -r, without a newline.
-S, --sort-keys
Sort the keys of objects.
--tab / --indent N
Indent with a tab / N spaces.
-C / -M
Colour on / off.

Input

-n, --null-input
Do not read input; build JSON from nothing.
-s, --slurp
Read all inputs into one array.
-R, --raw-input
Read lines as strings, not JSON.

Variables and checks

--arg NAME VALUE
Pass a shell value as a string $NAME.
--argjson NAME JSON
Pass a JSON value as $NAME.
-e, --exit-status
Exit 1 if the last result is false or null (for if).

Filters (inside the program)

.field / .a.b
A field; nested fields.
.[] / .[0]
Every item / the first item of an array.
select(COND)
Keep only items where COND is true.
map(F)
Apply F to every item of an array.
length / keys
Number of items / the keys of an object.
sort_by(.f) / group_by(.f)
Sort / group an array by a field.
to_entries / from_entries
Object ↔ list of {key, value}.
//
Default value: .tag // "latest".
@csv / @tsv / @base64d
Format as CSV / TSV; decode base64.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Pretty print an API answer

    curl -s https://api.github.com/repos/kubernetes/kubernetes | jq .
  2. 2Take one value into a shell variable

    STARS=$(curl -s https://api.github.com/repos/kubernetes/kubernetes | jq -r .stargazers_count)
    echo "$STARS"
  3. 3Names of all pods that are not Running

    kubectl get pods -A -o json | jq -r '.items[] | select(.status.phase != "Running") | "\(.metadata.namespace)/\(.metadata.name)"'
  4. 4Every container image in the cluster, once

    kubectl get pods -A -o json | jq -r '.items[].spec.containers[].image' | sort -u
  5. 5AWS: ID and type of running EC2 instances as a table

    aws ec2 describe-instances --filters Name=instance-state-name,Values=running | jq -r '.Reservations[].Instances[] | [.InstanceId, .InstanceType] | @tsv'
  6. 6Decode a Kubernetes secret

    kubectl get secret db -o json | jq -r '.data.password | @base64d'
  7. 7Change a value and write a new file

    jq '.replicas = 3 | .image.tag = "v1.4.0"' values.json > values.new.json
  8. 8Build JSON safely from shell values (no quoting bugs)

    jq -n --arg env prod --arg ver "$TAG" '{environment: $env, version: $ver}'
    {
      "environment": "prod",
      "version": "v1.4.0"
    }
  9. 9Use jq as a check in a script

    if curl -s localhost:8080/health | jq -e '.status == "ok"' >/dev/null; then echo healthy; fi
  10. 10Count items per group

    gh api repos/OWNER/REPO/issues --paginate | jq -r 'group_by(.state) | map({state: .[0].state, n: length})'
  11. 11Turn JSON lines (logs) into a table

    jq -r 'select(.level == "error") | [.time, .msg] | @tsv' app.log.json
  12. 12Terraform outputs into environment variables

    terraform output -json | jq -r 'to_entries[] | "export \(.key | ascii_upcase)=\(.value.value)"'

Watch out

  • Without -r strings keep their quotes: "web" instead of web.
  • Put the filter in single quotes so the shell does not change $ and |.
  • jq . file > file empties the file. Write to a new file, then mv.

Tips to remember

Exit codes

0OK.
1With -e: the last result was false or null.
2Usage or system error.
3Compile error in the filter.
5Could not read the input.