Pretty-printing JSON means transforming a compact, often difficult-to-read JSON string into a readable format—an indented, human-friendly representation.
Here are various ways to pretty-print json:
Using the json Module
The most efficient and easiest way to pretty-print JSON in Python is using the json module’s .dumps() method with the “indent” argument.
Set the indent parameter for formatting. You can set the 2, 4, or 6 as per your requirements.
import json
# Sample data: list of user information dictionaries
data = [
{"name": "David", "age": 30, "city": "New York"},
{"name": "Anna", "age": 25, "city": "Los Angeles"},
{"name": "Mike", "age": 28, "city": "Chicago"}
]
print("Before Pretty print: ", data)
# Convert data to a formatted JSON string
pretty_json = json.dumps(data, indent=2)
print("After Pretty print: ", pretty_json)
We passed indent=2, which means it formats the JSON data based on 2 spaces, making it human-readable and analyzable.
You can also sort the keys and preserve unicode characters by setting the sort_keys and ensure_ascii arguments.
import json
# Sample data: list of user information dictionaries
data = [
{"name": "David", "age": 30, "city": "New York"},
{"name": "Anna", "age": 25, "city": "Los Angeles"},
{"name": "Mike", "age": 28, "city": "Chicago"}
]
pretty_print = json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False)
print(pretty_print)
json.dump() to file
If you have JSON data and want to write it in an indented and proper format to a file, you can do so by using the json.dump() method with an indent argument.
import json
data = {
"users": [
{"id": 101, "name": "Krunal", "active": True},
{"id": 201, "name": "Ankit", "active": False}
],
"count": 2
}
with open("output_pretty.json", "w", encoding="utf-8") as fp:
json.dump(
data,
fp,
indent=4, # 4 spaces per level
sort_keys=True, # alphabetize the keys
ensure_ascii=False, # allow non‑ASCII characters unescaped
separators=(',', ': ') # tweak spacing around separators
)
Here is the written output_pretty.json file:
Command-Line Pretty Printing
There is a special module that you can use to pretty-print json in the command line or terminal. That module is “json.tool”. You can directly print the pretty-print json to the terminal from any file.
Go to the terminal and type the command below:
python -m json.tool output_pretty.json
Since we already have the output_pretty.json file, we can see the following output in the terminal.
Third-Party Libraries
For faster alternatives, consider using the ujson or simplejson library.
You need to install the ujson library to use its .dumps() method.
pip install ujson
Now, you can use its method.
import ujson
data = {
"users": [
{"id": 101, "name": "Krunal", "active": True},
{"id": 201, "name": "Ankit", "active": False}
],
"count": 2
}
pretty_json = ujson.dumps(data, indent=4)
print(pretty_json)
pprint module
The pprint() function from the pprint module prints your JSON data in a more readable format and can sort the keys alphabetically. Ensure that you install the pprint module first and then utilize its methods.
import json
from pprint import pprint
data = [
{"name": "David", "age": 30, "city": "New York"},
{"name": "Anna", "age": 25, "city": "Los Angeles"},
{"name": "Mike", "age": 28, "city": "Chicago"}
]
print("Before Pretty print: ", data)
# Pretty print JSON using pprint
print("After Pretty print: ")
pprint(data, indent=2, sort_dicts=False)
# Output:
# Before Pretty print: [{'name': 'David', 'age': 30, 'city': 'New York'}, {'name': 'Anna', 'age': 25, 'city': 'Los Angeles'}, {'name': 'Mike', 'age': 28, 'city': 'Chicago'}]
# After Pretty print:
# [ {'name': 'David', 'age': 30, 'city': 'New York'},
# {'name': 'Anna', 'age': 25, 'city': 'Los Angeles'},
# {'name': 'Mike', 'age': 28, 'city': 'Chicago'}]
That’s all!







