Write Dict to JSON File

Serialize a Python dictionary to a JSON file.

Code

Python
import json

data = {"name": "Alice", "scores": [90, 85, 88]}
with open("output.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2)

print("Written successfully")

Line-by-line explanation

Expected output

Written successfully

Related snippets

Related DuskTools