Settings, caches, fixtures and exported data are commonly stored as JSON files.
For files, use the variants without the s:
json.dump(obj, f)writes JSON to an open text file.json.load(f)reads and parses a whole file.
Open the file with an explicit encoding. JSON is defined as UTF-8, but open() without encoding= uses the platform's locale encoding, which on Windows may still be a legacy code page. open(path, "w", encoding="utf-8") is portable.
with open("config.json", encoding="utf-8") as f:
config = json.load(f)A JSON file holds exactly one value, usually an object or array. You cannot append a second object to the end of it and still load it with json.load; for append-only logs, use the JSON Lines format with one object per line.
json.load reads the whole file into memory before parsing, which is fine for configs and moderate data. For multi-gigabyte files, use JSON Lines or a streaming parser.
If you write a file other people or tools will read, consider indent=2 for readability. If a crash mid-write would be harmful, write to a temporary file and os.replace it into place.
Example
import json
settings = {"theme": "dark", "font_size": 14, "recent": ["a.txt", "b.txt"]}
with open("settings.json", "w", encoding="utf-8") as f:
json.dump(settings, f, indent=2)
with open("settings.json", encoding="utf-8") as f:
loaded = json.load(f)
print(loaded == settings) # True
print(loaded["recent"][-1]) # b.txt
print(open("settings.json", encoding="utf-8").read().splitlines()[1])
# "theme": "dark",Remember
json.dump/json.loadwork on file objects;dumps/loadson strings- Open JSON files with
encoding="utf-8" - A JSON file holds one value; use JSON Lines for appendable records
Watch out for
- Appending a second JSON object to an existing .json file, which makes it unparseable.