NV ToolkitFormat · Validate · Convert
Guide

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.

By NaveenKumar T · Software engineer · builds and maintains NV Toolkit Published 2026-06-04 2 min read

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.

python
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.

python
# 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.json

Writing 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.

python
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.

How this was tested: The Python examples were run with Python 3.12 on 2026-09-10.

Sources

  1. Python docs — json: JSON encoder and decoder
  2. Python docs — pprint: Data pretty printer
  3. RFC 8259 — The JSON Data Interchange Format

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 code

Keep working

Related tools