← All posts
postMay 25, 2026
Python: write a list of records to JSON
#python#json#file-io
python
import json
users = [
{"id": 1, "name": "alice"},
{"id": 2, "name": "bob"}
]
with open("users.json", "w") as f:
json.dump(users, f, indent=2)
# Read it back
with open("users.json") as f:
loaded = json.load(f)
print(loaded)JSON is the standard format for exchanging data between systems. Most APIs return JSON, configuration files often live in JSON, and many data pipelines store intermediate results as JSON.
Pythons json module turns lists and dicts into JSON text and back. The two functions you will use 95 percent of the time are json.dump() (write to a file) and json.load() (read from a file). Always pass indent=2 when you want the output to be human-readable.