Parsing a JSON string
import json
data = json.loads('{"name": "Ada", "age": 36}')
print(data["name"]) # Ada
json.loads() (note the "s" for "string") parses a JSON string into a Python dict or list. To read directly from an open file, use json.load(file_object) instead — no "s," and no need to read the file into a string first.
Converting Python to a JSON string
payload = {"name": "Ada", "active": True}
print(json.dumps(payload, indent=2))
json.dumps() converts a Python object into a JSON string; pass indent=2 to pretty-print it. To write straight to a file, use json.dump(obj, file_object).
Type mapping
dict → object, list/tuple → array, str → string, int/float → number, True/False → true/false, None → null. This mapping is what makes JSON such a natural fit for Python specifically — it mirrors Python's own built-in data structures almost one-to-one.
Handling parse errors
Invalid JSON raises json.JSONDecodeError, which includes the line and column of the problem — wrap json.loads() in a try/except block when parsing untrusted input. If you want to catch and inspect malformed JSON before writing any Python, paste it into our JSON Validator first.