JSON Pretty Print in Python: json.dumps() with indent
Python's built-in json module makes pretty printing straightforward with the indent parameter. Here is everything you need to know to format JSON in Python.
Basic Pretty Printing with json.dumps
import json; print(json.dumps(data, indent=4)) is the canonical Python one-liner for pretty printing a Python dict or list as JSON. The indent parameter sets the number of spaces per indentation level. Python conventions typically use 4 spaces, though 2 is also common in web-facing code.
json.dumps() serializes a Python object to a JSON-formatted string. Combined with indent, it produces a multi-line, indented string that can be printed, logged, written to a file, or returned from a function. The output is always valid JSON regardless of the indent value used.
Sorting Keys with sort_keys
json.dumps(data, indent=2, sort_keys=True) sorts the output keys alphabetically. This is valuable for reproducible output — when the same data structure is serialized multiple times, sorted keys guarantee the same output each time, making diffs and tests reliable.
Sort keys are especially useful in configuration management and infrastructure-as-code workflows where JSON config files are stored in version control. Sorted, formatted output produces minimal, meaningful diffs when values change.
Writing Pretty JSON to a File
To write pretty-printed JSON to a file, use json.dump() (without the s) which writes directly to a file object: with open("output.json", "w") as f: json.dump(data, f, indent=2). This is more memory-efficient than json.dumps() for large objects because it streams to the file rather than building the entire string in memory.
When reading a JSON file, modifying it, and writing it back, always use the same indent value to avoid whitespace-only diffs in version control. If the original file uses 2-space indentation, preserve that.
Pretty Printing from the Command Line
Python's json.tool module provides a command-line pretty printer: python3 -m json.tool input.json outputs formatted JSON to stdout. This requires no additional installation and works on any system with Python 3. Add --indent 2 to control the indent size.
Combined with curl, this creates a powerful API inspection workflow: curl -s https://api.example.com/data | python3 -m json.tool pipes the API response through the formatter and prints readable output to the terminal. This is the quickest way to inspect a JSON API from the command line.
Try JSON Pretty Print Free Online
No sign-up required. 100% client-side — your data never leaves your browser.
Open JSON Pretty Printarrow_forwardFrequently Asked Questions
What is the difference between json.dumps() and json.dump()?
json.dumps() returns a string. json.dump() writes directly to a file-like object. Use dumps() when you need a string (for logging, HTTP responses, etc.) and dump() when writing to a file.
How do I pretty print JSON with Unicode characters in Python?
By default, json.dumps() escapes non-ASCII characters as \uXXXX sequences. To preserve Unicode, use ensure_ascii=False: json.dumps(data, indent=2, ensure_ascii=False).
Can I pretty print JSON with comments in Python?
The built-in json module does not support comments. To parse JSON with comments, use a library like json5 (pip install json5) which supports the JSON5 format including comments.