Pretty Print JSON in Python: Every Method That Matters
One argument turns Python's compact JSON into readable output: indent. Here's the complete toolkit — in code, from the command line, and the flags that handle sorting, Unicode, and files properly.
In short
Pretty print JSON in Python by passing indent=2 to json.dumps() (or json.dump() for files); add sort_keys=True for deterministic key order and ensure_ascii=False to keep non-ASCII characters readable. From the shell, pipe any file through python -m json.tool for the same result with no script. Avoid pprint for JSON — it prints Python reprs, not valid JSON.
The standard answer: json.dumps(obj, indent=2)
Python's built-in json module does everything. indent controls the formatting; sort_keys=True gives deterministic key order (essential for diffs and tests); ensure_ascii=False keeps non-ASCII characters readable instead of \uXXXX-escaping them.
import json
data = {"name": "Ada", "tags": ["math", "computing"], "born": 1815}
print(json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False))
# {
# "born": 1815,
# "name": "Ada",
# "tags": ["math", "computing" ...
#
# Already have a JSON *string*? Parse, then re-dump:
pretty = json.dumps(json.loads(raw_string), indent=2)From the command line: python -m json.tool
No script needed — the json module is also a CLI. Pipe anything through it: cat data.json | python -m json.tool, or curl an API straight into it. Python 3.9+ accepts --sort-keys and an output file argument. It validates as it formats, so broken JSON fails loudly with a position instead of printing garbage.
# shell
curl -s https://api.example.com/users | python -m json.tool
python -m json.tool messy.json pretty.json # file in, file out
python -m json.tool --sort-keys data.jsonWriting pretty files (and what to avoid)
json.dump (no s) writes straight to a file handle with the same options — always pass encoding='utf-8' when opening. Avoid pprint for JSON work: it prints Python reprs (single quotes, True/None), which LOOKS like JSON but won't parse as JSON — running suspect output through our JSON Validator confirms it instantly. If the output's destination is anything but human eyes during debugging, stay in the json module.
with open("out.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# pprint trap — this is NOT valid JSON:
# {'name': 'Ada', 'active': True, 'advisor': None}When you don't want to write any code
Debugging someone else's payload, checking a config, sharing formatted output in a ticket — for one-off jobs a browser tool beats a REPL. Our JSON Formatter does the same parse-and-indent (plus validation with exact error positions, tree exploration, and conversion) with zero setup, entirely client-side.
Sources
Frequently asked questions
What's the difference between json.dumps and json.dump?
dumps returns a string ('s' for string); dump writes directly to a file object. Same formatting options on both — indent, sort_keys, ensure_ascii — choose by destination.
Why does my output show \u00e9 instead of é?
ensure_ascii defaults to True, escaping every non-ASCII character for maximum transport safety. Pass ensure_ascii=False for human-readable output — correct and standard as long as you write UTF-8, which you should.
Can json.tool handle huge files?
Yes — it streams reasonably and handles multi-hundred-MB files where editors choke. For exploring (rather than just reformatting) something that large, a tree viewer that lazily renders collapsed branches is the friendlier instrument.
For one-off jobs, paste the payload into our JSON Formatter — indent, sort keys, validate and explore, no Python session required.
Format without codeKeep working
Related tools
JSON Formatter
Format, beautify, and validate JSON instantly in a professional VS Code-style editor — free, fast, and 100% private.
JSON Sorter
Recursively sort every object's keys A→Z — arrays keep their order — for stable diffs, clean reviews, and canonical configs.
JSON Validator
Strict RFC 8259 validation with the exact line and column of every error — plus one-click repair for broken JSON.
JSON to Python
Paste a JSON or XML sample — the format is auto-detected — and get typed Python models with names, nesting and nullability inferred for you.
JSON Pretty Print
Turn dense single-line JSON into beautifully indented, readable output — your choice of indentation, with optional alphabetical key sorting.