📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Working with JSON

Working with JSON

5 min read
json.dumps() converts Python data to a JSON string and json.loads() parses JSON. Use json.dump() and json.load() for files. Handle json.JSONDecodeError when parsing input that may not be valid JSON.

JSON in Python

import json

# Parse JSON string
data = json.loads('{"name":"Alice","age":28}')
print(data["name"])  # Alice

# Convert to JSON string
json_str = json.dumps(data, indent=2)

# Read/write JSON file
with open("data.json") as f:
    data = json.load(f)
with open("out.json", "w") as f:
    json.dump(data, f, indent=2)