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

Make JSON readable in terminals, logs, and docs.

Pretty-print with jq

Format JSON with indentation and colors in many terminals.

bashANYjsonjqpretty-print
bash
jq . data.json

The most common CLI workflow for readable JSON.

Pretty-print with Python

Format JSON using the standard library.

bashANYjsonpythonpretty-print
bash
python -m json.tool data.json

A portable fallback when `jq` is unavailable.

Pretty-print with sorted keys

Sort object keys for predictable diffs.

bashANYjsonpythonsort-keys
bash
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

Stable key order is useful in reviews and version control.

Minify JSON

Remove unnecessary whitespace for compact transport or embedding.

Compact JSON with jq

Emit a minified one-line form.

bashANYjsonjqminify
bash
jq -c . data.json

Useful for logs, payload fixtures, and embedding JSON in scripts.

Minify with Python

Serialize JSON without extra whitespace.

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

The `separators` option removes spaces after commas and colons.

Pretty-print an API response

Pipe JSON from an HTTP request into jq.

bashANYjsoncurljqapi
bash
curl -s https://api.example.com/users | jq .

A practical workflow when inspecting REST responses.

Recommended next

No recommendations yet.