Guide · Updated 2026-07-29

JSON in Python

Python's built-in json module handles reading, writing, and parsing JSON with no extra installs. Here's everything you need for typical use.

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.

Frequently asked questions

How do I parse a JSON string in Python?

Use json.loads(text) to parse a JSON string into a Python dict or list. To read directly from a file, use json.load(file_object) instead.

How do I convert a Python object to a JSON string?

Use json.dumps(obj) to convert a Python dict or list into a JSON string. Pass indent=2 to pretty-print it, or json.dump(obj, file_object) to write straight to a file.

What Python types map to which JSON types?

dict maps to a JSON object, list/tuple to a JSON array, str to a string, int/float to a number, True/False to true/false, and None to null.