Pretty-Print JSON Data to a File using Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Pretty-printing JSON to a file in Python is straightforward with the standard json module, but the details matter if you care about readability, stable diffs, or safe writes. The right implementation depends on whether the file is just for debugging or is part of a repeatable build, configuration, or state-management workflow.
Use json.dump With Indentation
The basic way to write readable JSON is to call json.dump with an indent value.
This already produces readable multi-line JSON. For many local debugging tasks, that is enough.
Make the Output Stable for Diffs
If the file will be committed to version control or compared across runs, deterministic ordering matters as much as indentation.
The trailing newline and key sorting help keep diffs clean and predictable.
Handle Non-JSON Python Types Explicitly
Objects such as datetime, Decimal, or custom classes are not serializable by default. If those appear in the data, provide a conversion function.
Do not ignore these failures or silently coerce them without deciding what representation is correct for the consumer of the file.
Use Safer Writes for Important Files
If the file is important state rather than disposable debug output, writing directly to the destination path can leave a corrupted partial file if the process stops mid-write. A safer pattern is to write to a temporary file and then replace the target atomically.
This is a better default for configuration, cache, or state files that another process may read.
Reformat Existing JSON Cleanly
A common task is to read minified or inconsistently formatted JSON and rewrite it in one canonical style.
That is a simple way to normalize generated files or prepare machine-produced JSON for human review.
For many scripts, the standard library is enough. You usually do not need a third-party formatter unless the workflow also requires schema validation, comments, or other non-standard JSON features.
Common Pitfalls
A common mistake is assuming indentation alone makes the output good enough for long-term use. If the file participates in tests or code review, stable key ordering often matters too.
Another issue is forgetting that some Python objects are not JSON-native. Pretty printing cannot fix serialization errors by itself.
Developers also often overwrite important files directly when they should be using a safer temporary-file pattern.
Finally, pretty formatting does not guarantee semantic correctness. The JSON can be beautifully indented and still contain the wrong data.
Summary
- Use
json.dumpwithindentto write readable JSON files. - Add
sort_keys=Trueand a trailing newline when stable diffs matter. - Provide a custom serializer for non-JSON Python types.
- Use atomic replace patterns when file integrity matters.
- Treat pretty printing as formatting, not as a substitute for validation.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.