How to dump a dict to a JSON file?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Writing a Python dictionary to a JSON file is one of the simplest persistence tasks in the standard library. The basic tool is json.dump, but quality comes from handling encoding, formatting, and non-JSON-native values correctly. A small amount of care keeps the output readable and avoids surprises later when the file is loaded again.
Use json.dump with a File Handle
The standard pattern is to open a file in text mode and pass both the dictionary and the file object to json.dump.
This writes compact JSON to profile.json. The with block ensures the file closes cleanly even if an exception occurs.
Make the Output Human-Readable
For config files, fixtures, or debug output, compact JSON is harder to inspect. Adding indentation makes the file much easier to read.
This is usually the right default for files that humans may open directly.
Preserve Non-ASCII Characters When Needed
By default, Python escapes non-ASCII characters. That is valid JSON, but it can make output harder to read for real text data. Setting ensure_ascii=False keeps UTF-8 characters readable.
Because the file is opened with UTF-8 encoding, the text is written cleanly and remains portable.
Know What JSON Can and Cannot Store
JSON supports objects, arrays, strings, numbers, booleans, and null. A normal Python dict maps cleanly to a JSON object, but not every Python value is serializable by default.
For example, this will fail:
To handle custom or unsupported values, provide a conversion function:
This is the usual way to serialize datetimes, decimals, and application-specific types.
Dump to a String First When Helpful
Sometimes you want the JSON text before writing it, for logging or testing. In that case, json.dumps is the string-producing version.
For direct file output, json.dump is simpler. json.dumps is useful when the JSON string itself has value before it reaches the filesystem.
Write Safely for Important Files
If the file is important and partial writes would be a problem, writing to a temporary file first and then replacing the target can be safer than writing directly. That pattern matters for config updates and caches used by multiple processes.
The important idea is not that JSON needs special handling. It is that file replacement can be safer than in-place overwrite when corruption matters.
Common Pitfalls
- Forgetting to open the file with
encoding="utf-8"when text data may contain non-ASCII characters. - Writing unreadable one-line JSON when the file is meant for humans.
- Assuming every Python object can be serialized without a custom converter.
- Confusing
json.dumpwithjson.dumps. - Writing directly to a critical file when a safer replace strategy would reduce corruption risk.
Summary
- Use
json.dumpto write a dictionary directly to a JSON file. - Add
indentwhen humans may read the file. - Use
ensure_ascii=Falsewith UTF-8 when readable non-ASCII text matters. - Supply a
defaultconverter for unsupported Python types. - Use
json.dumpsonly when you specifically need the JSON as a string first.
Related reading
- How to dynamically build a JSON object?
- How to Dynamodb send message to SQS
- How to easily remember Red-Black Tree insert and delete?
- How to efficiently check if a list of consecutive numbers is missing any elements
- How to dynamically create a class?
- How to dynamically load a Python class
- How to efficiently compare two unordered lists not sets?
- How to efficiently compare two unordered lists not sets?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.