DEV Community

Cover image for How to Pretty Print JSON in Python?
Mateen Kiani
Mateen Kiani

Posted on • Originally published at milddev.com

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)
Enter fullscreen mode Exit fullscreen mode

Add more options:

pretty_sorted = json.dumps(data, indent=2, sort_keys=True)
print(pretty_sorted)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Then write with indentation:

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

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)