Skip to main content
Data

How to parse JSON in Python

Quick answer

Use json.loads(text) for a string, json.load(file) for a file object. Both return the parsed Python object (dict, list, str, int, etc.).

import json

text = '{"name": "Alice", "age": 30, "skills": ["python"]}'
data = json.loads(text)
print(data["name"])       # 'Alice'
print(data["skills"][0])  # 'python'

json.loads takes a str (or bytes) and returns whatever the JSON's top-level type maps to: a JSON object becomes a Python dict, an array becomes a list, a string becomes str, etc. The reverse is json.dumps(obj) which serialises a Python value back to a JSON string.

When the JSON is stored in a file, use json.load(file) β€” same behaviour, but takes a file-like object instead of a string. Always open the file with encoding='utf-8' since JSON is spec'd as UTF-8.

For 3x-10x speed on large payloads or high-QPS servers, drop-in-replace json with orjson (pip install orjson).

Variations

Load from a file

with open("data.json", encoding="utf-8") as f:
    data = json.load(f)

json.load takes the file object, not its path.

Handle a parse error

try:
    data = json.loads(text)
except json.JSONDecodeError as e:
    print("bad json at", e.pos)

e.pos, e.lineno, e.colno pinpoint the exact byte.

Fast JSON via orjson

import orjson
data = orjson.loads(text)

Same API, 3-10x faster. Returns bytes from dumps() β€” decode() if you need str.

Practise this in your browser

Every how-to has a live lesson. Free tier, no card.

Start free β†’

Unlock every lesson

Pro β€” $12/mo, unlimited AI, cancel any time.

See Pro plans β†’

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.

Related how-tos