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 formatter at jsonconsole.com 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
What is the difference between json.load() and json.loads()?
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 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.
What is the most efficient way to handle large JSON files in Python?
Learn efficient ways to handle large JSON files in Python. Discover streaming parsers, ijson, and memory-efficient techniques.
Still have questions?
Can't find the answer you're looking for? Please reach out to our support team.