ubuntu, updated
jq cheat sheet
Read, filter and reshape JSON on the command line: pretty-print, pick fields, filter arrays, and turn API output into lists and CSV.
What it is
APIs, cloud CLIs, Docker and many tools speak JSON. jq reads JSON and lets you pick and reshape the parts you need.
sudo apt install jq
All examples use this input (servers.json):
{
"env": "prod",
"servers": [
{ "name": "web01", "ip": "10.0.0.11", "role": "web", "cpu": 4, "tags": ["nginx"] },
{ "name": "web02", "ip": "10.0.0.12", "role": "web", "cpu": 4, "tags": ["nginx"] },
{ "name": "db01", "ip": "10.0.0.21", "role": "db", "cpu": 8, "tags": ["postgres"] }
]
}
Picking values
| What | Command | Result |
|---|---|---|
| Pretty-print | jq . servers.json | formatted, colored JSON |
| One field | jq .env servers.json | "prod" |
| Without quotes (raw) | jq -r .env servers.json | prod |
| One array element | jq '.servers[0]' servers.json | first server |
| Every element | jq '.servers[]' servers.json | each server |
| One field of every element | jq -r '.servers[].name' servers.json | web01 web02 db01 |
| Count | jq '.servers | length' servers.json | 3 |
| List keys | jq 'keys' servers.json | ["env","servers"] |
-r (raw output) is what you want almost always when the result goes into a script or another command.
Filtering
| What | Command |
|---|---|
| Where a field equals a value | jq '.servers[] | select(.role == "db")' |
| Number comparison | jq '.servers[] | select(.cpu >= 8)' |
| Text contains | jq '.servers[] | select(.name | contains("web"))' |
| Array contains | jq '.servers[] | select(.tags | index("nginx"))' |
| Field exists / is not null | jq '.servers[] | select(.ip != null)' |
| Filter, then pick one field | jq -r '.servers[] | select(.role == "web") | .ip' |
Reshaping
| What | Command |
|---|---|
| New object with chosen fields | jq '.servers[] | {name, ip}' |
| Rename fields | jq '.servers[] | {host: .name, addr: .ip}' |
| Keep it as an array | jq '[.servers[] | {name, ip}]' |
| Two fields as text | jq -r '.servers[] | "\(.name) \(.ip)"' |
| Tab-separated | jq -r '.servers[] | [.name, .ip] | @tsv' |
| CSV | jq -r '.servers[] | [.name, .ip, .role] | @csv' |
| Sort | jq '.servers | sort_by(.cpu)' |
| Sum | jq '[.servers[].cpu] | add' |
| Group and count | jq '.servers | group_by(.role) | map({role: .[0].role, count: length})' |
Changing JSON
jq '.env = "staging"' servers.json # set a value
jq 'del(.servers[0])' servers.json # delete an element
jq --arg ip "10.0.0.99" '.servers[0].ip = $ip' servers.json # use a shell value safely
jq '.env = "staging"' servers.json > tmp && mv tmp servers.json # "in place"
jq never changes the file itself. Write to a temp file and move it.
Real-world examples
# Public IP and location
curl -s https://ipinfo.io | jq -r '.ip, .city'
# Names and states of all Docker containers
docker inspect $(docker ps -aq) | jq -r '.[] | "\(.Name) \(.State.Status)"'
# IPs of all interfaces (ip can output JSON!)
ip -j addr | jq -r '.[] | .ifname + " " + (.addr_info[]? | select(.family=="inet") | .local)'
# Disks as JSON
lsblk -J | jq -r '.blockdevices[] | "\(.name) \(.size)"'
# Loop over JSON results in Bash
jq -r '.servers[].ip' servers.json | while read -r IP; do ping -c1 -W1 "$IP"; done
Many tools have a JSON output option (ip -j, lsblk -J, docker inspect, systemctl --output=json, ss does not). Combined with jq that is much more robust than cutting text with awk.