JSON Tools
Prefer jq for querying JSON; fall back to Python's json if jq isn't installed
(command -v jq to check).
Pretty-print / validate
jq . data.json # pretty-print (errors if invalid)
python3 -m json.tool data.json # stdlib fallback
Extract a field / path
jq -r '.user.name' data.json
jq -r '.items[].id' data.json # one id per line
jq -r '.items[] | select(.active)' data.json
Reshape into a new object / CSV
jq '{id: .id, who: .user.name}' data.json
jq -r '.rows[] | [.id, .name] | @csv' data.json
Python fallback for the same tasks
python3 - <<'PY'
import json
d = json.load(open('data.json'))
print([it['id'] for it in d['items'] if it.get('active')])
PY
Guidance
jq -rstrips quotes for raw string output; drop-rto keep valid JSON.- For huge files, filter early (
jq '.items[] | select(...)') instead of loading all into memory in Python. - When piping an API response, validate first — a truncated/HTML error page is not JSON.
- Show the extracted result, not the entire input.