How to Pretty Print JSON in Python?

Mateen Kiani

Mateen Kiani

Published on Mon Aug 11 2025·3 min read

how-to-pretty-print-json-in-python

Introduction

Working with JSON in Python is straightforward thanks to the built-in json module. However, when you need to read or share JSON data, the compact output can be hard to scan. A quick fix is to pretty print JSON with indentation and sorted keys, making it far more readable for debugging or documentation.

In this guide, we'll explore several ways to pretty print JSON in Python. We'll cover the basics of the json module, show how to read and write JSON files with pretty output, discuss the pprint module, and introduce third-party libraries for more control and performance.

Why Pretty Printing Matters

JSON is key in APIs, config files, and data exchange. While machine readability favors compact JSON, humans benefit from structure:

  • Indentation highlights hierarchy.
  • Sorted keys help locate fields.
  • Line breaks prevent long, unwieldy lines.

When troubleshooting API responses or reviewing configuration files, pretty printed JSON lets you spot missing or misplaced data quickly. It also improves logs, making issues stand out. Ultimately, readable data saves time and reduces errors.

Using json.dumps with indent

The simplest way to pretty print JSON is with the json.dumps function:

import json
data = {'name': 'Alice', 'age': 25, 'city': 'New York'}
pretty = json.dumps(data, indent=4)
print(pretty)

Add more options:

pretty_sorted = json.dumps(data, indent=2, sort_keys=True)
print(pretty_sorted)

Parameters:

  • indent: number of spaces per level.
  • sort_keys: sort dictionary keys alphabetically.
  • ensure_ascii: set to False to allow unicode.

These options make output clear and consistent.

Pretty Printing JSON Files

Reading and writing JSON files with pretty printing is just as easy. Start by loading your data:

import json
with open('data.json', 'r') as f:
data = json.load(f)

Then write with indentation:

with open('pretty_data.json', 'w') as f:
json.dump(data, f, indent=4)

Tip: Use separators=(',', ': ') to control spacing between items.

For more details on dumping JSON to files, see our guide: how to dump JSON to a file in Python.

Using pprint for JSON

The pprint module offers a general pretty printer for Python objects. It's handy for quick debugging:

from pprint import pprint
data = {'name': 'Alice', 'age': 25, 'languages': ['Python', 'JavaScript']}
pprint(data)

pprint output uses its own formatting rules. It may differ from json.dumps especially for nested structures.

Third-Party Libraries

For large data or advanced features, consider third-party tools:

  • rich: beautiful console rendering.
  • orjson: speed-focused JSON library with indent support.

Example with rich:

from rich import print_json
print_json(data=data)

rich highlights syntax and colors, making terminal output even clearer.

Performance Considerations

Pretty printing adds overhead, especially for large JSON. If speed matters:

  • Skip pretty print in production logs.
  • Use orjson or ujson for faster dumps.
  • Only format when needed, not for every request.

Measure with timeit to find the best option.

Conclusion

Pretty printing JSON in Python transforms compact, machine-centric data into readable output for humans. Using the built-in json module with indent and sort_keys meets most needs. For quick debugging, pprint works well, and for more powerful features, explore libraries like rich or orjson. Choose the right tool based on your workflow: readability, performance, or level of detail. Clear data leads to efficient development and easier troubleshooting.


Mateen Kiani
Mateen Kiani
kiani.mateen012@gmail.com
I am a passionate Full stack developer with around 4 years of experience in MERN stack development and 1 year experience in blockchain application development. I have completed several projects in MERN stack, Nextjs and blockchain, including some NFT marketplaces. I have vast experience in Node js, Express, React and Redux.