Python Glossary Русский

all Python 3jsonfunction

json.loads

json

Parse a JSON string into a Python object.

Syntax

json.loads(s)

JSON is not a Python literal: true/false/null instead of True/False/None, double quotes only, no trailing commas — this is what usually triggers json.JSONDecodeError (a ValueError subclass). Catch it by that name and read its .pos, .lineno and .colno to point at the exact spot that broke. Never parse JSON with eval: it is a security hole and it chokes on true/null anyway.

Examples

import json
print(json.loads('{"x": 5, "y": true}'))   # → {'x': 5, 'y': True}
print(json.loads('[1, 2, 3]'))              # → [1, 2, 3]
data = json.loads('{"user": {"name": "Bob", "scores": [4, 5]}}')
print(data['user']['scores'][1])   # → 5
print(json.loads("{'x': 1}"))   # → JSONDecodeError (нужны двойные кавычки)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy