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
Input
Variables and checks
Filters (inside the program)
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Pretty print an API answer
curl -s https://api.github.com/repos/kubernetes/kubernetes | jq .2Take one value into a shell variable
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)"'4Every container image in the cluster, once
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'6Decode a Kubernetes secret
kubectl get secret db -o json | jq -r '.data.password | @base64d'7Change a value and write a new file
jq '.replicas = 3 | .image.tag = "v1.4.0"' values.json > values.new.json8Build 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" }9Use jq as a check in a script
10Count items per group
gh api repos/OWNER/REPO/issues --paginate | jq -r 'group_by(.state) | map({state: .[0].state, n: length})'11Turn JSON lines (logs) into a table
jq -r 'select(.level == "error") | [.time, .msg] | @tsv' app.log.json12Terraform outputs into environment variables
terraform output -json | jq -r 'to_entries[] | "export \(.key | ascii_upcase)=\(.value.value)"'
Nothing matches this filter.
Watch out
- Without
-rstrings keep their quotes:"web"instead ofweb. - Put the filter in single quotes so the shell does not change
$and|. jq . file > fileempties the file. Write to a new file, thenmv.
Tips to remember
- Try filters live:
kubectl get pods -o json | jq '.items[0] | keys'shows which fields exist. gronandjqtogether:gronshows the path of every value.
Exit codes
0 | OK. |
1 | With -e: the last result was false or null. |
2 | Usage or system error. |
3 | Compile error in the filter. |
5 | Could not read the input. |