JSON Formatting, Minifying, and Pretty-Printing

Commands and patterns for compact JSON, human-readable formatting, stable sorting, and terminal-friendly inspection.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Pretty-printing
Pretty-print with jq
jq . data.json

# Format JSON with indentation and colors in many terminals.

Pretty-print with Python
python -m json.tool data.json

# Format JSON using the standard library.

Pretty-print with sorted keys
python - <<'PY'
import json
from pathlib import Path
p = Path('data.json')
obj = json.loads(p.read_text())
print(json.dumps(obj, indent=2, sort_keys=True))
PY

# Sort object keys for predictable diffs.

## Minify JSON
Compact JSON with jq
jq -c . data.json

# Emit a minified one-line form.

Minify with Python
python - <<'PY'
import json
from pathlib import Path
p = Path('data.json')
obj = json.loads(p.read_text())
print(json.dumps(obj, separators=(',', ':')))
PY

# Serialize JSON without extra whitespace.

Pretty-print an API response
curl -s https://api.example.com/users | jq .

# Pipe JSON from an HTTP request into jq.

Recommended next

No recommendations yet.