Last modified: May 28, 2026
Python JSON Dump Pretty Print Guide
Working with JSON in Python is common. Raw JSON can be hard to read. You need a way to make it look clean. This is where pretty printing helps.
Pretty printing means formatting JSON with spaces and line breaks. It makes data human-friendly. Python’s json module has two main functions for this: json.dumps() and json.dump(). Both can produce pretty output.
What is Pretty Printing in Python?
Pretty printing adds indentation to JSON strings. Without it, JSON looks like one long line. With it, you see nested structures clearly.
For example, raw JSON looks like this:
import json
data = {"name": "Alice", "age": 30, "hobbies": ["reading", "coding"]}
print(json.dumps(data))
Output:
{"name": "Alice", "age": 30, "hobbies": ["reading", "coding"]}
This is hard to scan. Now add pretty printing:
print(json.dumps(data, indent=4))
Output:
{
"name": "Alice",
"age": 30,
"hobbies": [
"reading",
"coding"
]
}
Much better. The indent parameter controls spacing. Use any integer value like 2, 4, or 8.
Using json.dumps() for Pretty Printing
The json.dumps() function converts a Python object to a JSON string. To make it pretty, add the indent argument.
Here is a complete example:
import json
# Sample data
person = {
"name": "Bob",
"age": 25,
"address": {
"street": "123 Main St",
"city": "Springfield"
},
"skills": ["Python", "Java"]
}
# Pretty print with indent=2
pretty_json = json.dumps(person, indent=2, sort_keys=True)
print(pretty_json)
Output:
{
"address": {
"city": "Springfield",
"street": "123 Main St"
},
"age": 25,
"name": "Bob",
"skills": [
"Python",
"Java"
]
}
Notice sort_keys=True sorts dictionary keys alphabetically. This is optional but helpful for consistency.
Using json.dump() for File Output
When you write JSON to a file, use json.dump(). It works the same way but writes directly to a file object.
Example:
import json
data = {"product": "Laptop", "price": 1200, "in_stock": True}
with open("product.json", "w") as f:
json.dump(data, f, indent=4)
print("File written successfully.")
This creates a file product.json with pretty formatted JSON. Open it and you will see:
{
"product": "Laptop",
"price": 1200,
"in_stock": true
}
Using json.dump() with indent is perfect for saving readable configuration files or API responses.
Customizing Pretty Print Output
You can control more than indentation. The separators parameter changes spacing between items. Default separators are (', ', ': '). For compact output, use (',', ':').
Example:
import json
data = {"a": 1, "b": 2}
# Default separators
print(json.dumps(data, indent=2))
# Custom separators (more compact)
print(json.dumps(data, indent=2, separators=(',', ': ')))
The ensure_ascii=False parameter keeps non-ASCII characters like emojis or accents. This is useful for international data.
data = {"message": "Hola, cómo estás?"}
print(json.dumps(data, indent=2, ensure_ascii=False))
Output:
{
"message": "Hola, cómo estás?"
}
Common Mistakes and Solutions
Beginners often forget the indent argument. Without it, output is not pretty. Always add indent when you want readability.
Another mistake is using json.dump() with a string instead of a file object. Remember: dump() needs a file handle, dumps() returns a string.
If your JSON has circular references, you will get an error. Use default parameter or cls to handle complex objects.
Real-World Use Case: API Response
When you fetch data from an API, it often comes as JSON. Pretty printing helps you debug and understand the structure.
import json
import requests
# Fetch data from a public API
response = requests.get("https://api.github.com")
data = response.json()
# Pretty print the response
print(json.dumps(data, indent=2, sort_keys=True))
This prints a readable version of the GitHub API response. You can see endpoints, URLs, and other details clearly.
Performance Considerations
Pretty printing adds overhead. For large JSON files (over 100 MB), avoid indent if speed matters. Use it only for small to medium data or debugging.
For production logs, consider writing compact JSON and using tools like jq to format it later. This saves storage space.
Conclusion
Python JSON pretty printing is simple. Use json.dumps() with indent for strings. Use json.dump() with indent for files. Customize with sort_keys, separators, and ensure_ascii. This makes your JSON clean and readable for humans. Practice with your own data to master it quickly.