
Python's json module makes JSON trivial.
Parse JSON string to Python dict:
import json
json_string = '{"name": "Alice", "age": 30}'
data = json.loads(json_string)
print(data["name"]) # AliceConvert Python dict to JSON string:
data = {"name": "Alice", "scores": [95, 87, 92]}
json_string = json.dumps(data, indent=2)
print(json_string)Read JSON from file:
with open("data.json", "r") as f:
data = json.load(f)Write JSON to file:
with open("output.json", "w") as f:
json.dump(data, f, indent=2)Type mapping:
JSON object → Python dict
JSON array → Python list
JSON string → Python str
JSON number → Python int or float
JSON true/false → Python True/False
JSON null → Python None
Reference:
TaskLoco™ — The Sticky Note GOAT