Python JSON
How do you pretty-print JSON in Python with indentation?
Pretty-print JSON in Python using json.dumps() with the indent parameter: json.dumps(data, indent=4) adds 4-space indentation. For 2-space indentation, use indent=2. Add sort_keys=True to alphabetically sort keys: json.dumps(data, indent=4, sort_keys=True). When writing to files, use json.dump() with the same parameters: json.dump(data, f, indent=4). For even prettier output, combine with ensure_ascii=False to preserve Unicode characters: json.dumps(data, indent=4, ensure_ascii=False). The pprint module can also display JSON nicely: pprint.pprint(json.loads(json_string)). For quick visualization without code, paste your JSON into our JSON Beautifier at jsonconsole.com/json-beautifier which formats it instantly. Pretty-printing is essential for debugging, logging, and making JSON human-readable during development. The formatted output helps identify structure issues and improves code reviews.
Last updated: December 23, 2025
Previous
How do I fix "Object of type datetime is not JSON serializable" in Python?
Next
How can I convert a JSON string into a custom Python object?
Related Questions
What is the difference between json.load() and json.loads()?
Learn the difference between json.load() and json.loads() in Python. Understand when to use each method for parsing JSON.
How do I fix "Object of type datetime is not JSON serializable" in Python?
Fix "datetime is not JSON serializable" error in Python. Learn how to serialize datetime objects using custom handlers.
How can I convert a JSON string into a custom Python object?
Learn how to convert JSON strings to custom Python objects. Master object_hook, dataclasses, and Pydantic for JSON deserialization.
Still have questions?
Can't find the answer you're looking for? Please reach out to our support team.