JSON is the lingua franca of modern IT. REST APIs speak JSON. Config files are JSON (or YAML, which is just JSON with fewer quotes). Cloud CLI tools spit JSON. Docker, Kubernetes, Terraform, AWS, Azure โ all of them talk JSON. If you can't read and manipulate JSON quickly, you're burning hours on copy-paste into online formatters.
This article shows you how to tame JSON in the terminal, which jq commands you actually need, and where the bitcalc JSON Formatter makes life easier.
jq: the Swiss Army knife for JSON
jq is to JSON what sed is to text โ but for structured data.
# AWS CLI: all EC2 instances with name and type
aws ec2 describe-instances | jq '.Reservations[].Instances[] | {name: .Tags[]? | select(.Key=="Name").Value, type: .InstanceType}'
# Docker: all running container IDs
docker ps --format json | jq -r '.ID'
# Terraform: all resource names from state
terraform show -json | jq '.values.root_module.resources[].name'
The five jq commands you need to know cold
jq '.'โ Pretty-print JSON. Or use the JSON Formatter in the browser.jq '.key'โ Extract a field.jq '.[]'โ Iterate over an array.jq 'select(.foo == "bar")'โ Filter.jq -rโ Raw output. Critical for shell pipelines.
jq '.' does โ in the browser, with one click. Paste JSON, click "Format", done. The validator instantly flags missing brackets.
JSON format pitfalls: the three things that always break
Trailing commas โ JSON forbids them. Comments โ JSON has none. Single quotes โ JSON requires double quotes. The JSON Formatter catches all three.
JSON Lines: the unknown log format
One JSON object per line. Docker logs, CloudTrail, Elasticsearch bulk API. jq handles it with -c (compact) and -s (slurp into array).
Real-world: making Terraform state readable
# All AWS instance IDs from state
terraform show -json | jq '.values.root_module.resources[] | select(.type == "aws_instance") | .values.id'
For quick visual inspection, paste the output into the JSON Formatter and scroll with syntax highlighting.