JSON Beautifier
Developer workflows3 min read

How to Pretty Print JSON in Python

Make a JSON file or API response readable using Python’s built-in json module. Learn the difference between parsing text, formatting an object, and writing a UTF-8 file.

Pretty print JSON in your browser

Start with a Python object

Use json.dumps to turn a Python dictionary or list into JSON text. The indent option adds line breaks and spaces. Use ensure_ascii=False when you want Unicode characters to remain readable, and allow_nan=False when you need strict JSON output.

This is useful for a small debugging snapshot. The example below uses an object authored in Python, so there is no separate parse step before formatting.

Format an object
import json

user = {"name": "Alice", "city": "東京", "active": True}
formatted = json.dumps(user, indent=2, ensure_ascii=False, allow_nan=False)
print(formatted)

Parse JSON text before formatting it

If your input is JSON text, parse it with json.loads first. Passing that raw string directly to json.dumps produces a quoted string containing JSON rather than a formatted document. That extra encoding layer is a common source of backslashes and escaped quotes.

After parsing, confirm that the result has the shape you expect. A response may be an array, a scalar, or an object wrapping the records inside another field.

JSON text to a formatted document
raw = '{"users":[{"name":"Alice"},{"name":"Bob"}]}'
data = json.loads(raw)
print(json.dumps(data, indent=2, ensure_ascii=False))

Read and write a JSON file

Read the source file with an explicit encoding and write the formatted result to a separate path. Keeping the input file makes it easier to compare the result or repeat the operation. Add a final newline for a clean text file.

For configuration changes, validate the file after your edit and before replacing a live configuration. Formatting only changes its presentation; it does not establish that its fields meet an application schema.

Write a separate UTF-8 file
from pathlib import Path

source = Path("response.json")
data = json.loads(source.read_text(encoding="utf-8"))
Path("formatted.json").write_text(
    json.dumps(data, indent=2, ensure_ascii=False, allow_nan=False) + "\n",
    encoding="utf-8",
)

Use the command line for a quick check

For a local file, Python’s json.tool module gives you a quick formatting and syntax-checking workflow without a separate script. The command below writes the result to a new file. A malformed document produces a parser error instead of a formatted output.

This approach is convenient for reviewing a downloaded response or a configuration file. If you need to inspect a single nested field, a tree viewer or a JSONPath query may be more helpful than printing the whole document.

Command line
python -m json.tool response.json formatted.json

Choose options deliberately

sort_keys=True produces a stable alphabetical key order, but may make a human-authored configuration less familiar. For a smaller serialized payload, separators=(",", ":") removes formatting rather than adding it. Keep those goals separate.

Python dictionaries, dates and custom classes are not interchangeable with JSON values. Decide how unsupported values should be represented instead of converting everything blindly to a string. If your input contains exact decimal values, use a parsing and serialization strategy that preserves the precision your application needs.

  • Use loads for JSON text and dumps for a Python value.
  • Use load and dump when working directly with open file objects.
  • Keep output Unicode readable with ensure_ascii=False.
  • Use allow_nan=False when generating strict JSON.
  • Try the browser formatter for a one-off document without installing Python.

Put the example to work

Use your own sample, check the result, and copy or download it. The tool processes your data locally in your browser.

Pretty print JSON in your browser

Reference documentation