October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Save a Python Dictionary as a JSON File in Python

Write a Python dictionary to a UTF-8 JSON file with json.dump(), then read it with json.load().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in json module: open a text file in write mode with UTF-8 encoding, then pass the dictionary and open file handle to json.dump(). Add indent=4 for a readable file.

Save a dictionary directly to a JSON file

This example writes a Python dictionary to data.json:

import json

my_dict = {"name": "Ada", "language": "Python"}

with open("data.json", "w", encoding="utf-8") as json_file:
    json.dump(my_dict, json_file, indent=4)

The with block closes the file when writing is finished. The resulting file contains valid JSON with indentation and line breaks, making it easier to inspect.

Load the JSON file back into Python

Use json.load() with an open file handle to read the saved data:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import json

with open("data.json", "r", encoding="utf-8") as json_file:
    loaded_dict = json.load(json_file)

For ordinary string-keyed dictionaries containing JSON-compatible values, loaded_dict will contain the data represented in the file. Python’s tutorial recommends explicitly using UTF-8 when reading and writing JSON text files: Python tutorial: Input and Output.

Choose readable, compact, or sorted output

These options affect presentation, not whether the dictionary can be written:

  • Readable: pass indent=4 to format nested structures across multiple lines.
  • Compact: omit indent for compact output, or use separators=(",", ":") for the most compact representation.
  • Sorted keys: add sort_keys=True if alphabetic key ordering is useful for reviewing diffs or comparing files.

For example, readable output with sorted keys is json.dump(my_dict, json_file, indent=4, sort_keys=True).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What Python values can be saved?

The JSON encoder handles dictionaries, lists, tuples, strings, numbers, booleans, and None using the conversions documented by Python. A tuple is represented as a JSON array, and None becomes JSON null. A value outside the supported conversions raises TypeError by default; supply a default conversion function or a custom encoder if you need to represent such objects. See the Python 3.13 json module reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Non-string dictionary keys

JSON object keys are strings. Python can convert certain non-string dictionary keys to strings during serialization, so loading the file may not recreate the original dictionary exactly. If key types matter, convert keys deliberately and account for that change when loading.

NaN and infinity

By default, Python permits NaN, Infinity, and -Infinity in its output, although these are not valid JSON numbers under the JSON specification. Use allow_nan=False if the file must reject those values rather than write them.

Common mistakes to avoid

  • Passing a filename to json.dump(): its second argument must be a writable file-like object, not a path string. Open the file first, as in the examples.
  • Setting encoding on json.dump(): specify encoding="utf-8" in open(). The encoding argument to json.dump() was removed in Python 3.9.
  • Appending repeated dumps as if they were separate documents: JSON is not a framed protocol. Repeated calls to dump() on the same file can produce invalid JSON; write one complete JSON document per file instead.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.