Python Glossary Русский

all Python 3jsonfunction

json.load

json

Read JSON from a file object and return a Python object.

Syntax

json.load(fp)

Despite taking a file object, this is not streaming: load is essentially loads(fp.read()) and holds the whole document in memory, so huge files call for JSON Lines (parsed line by line) or a third-party parser like ijson. The file must hold exactly one JSON document — two objects back to back, or any leftover text after the first one, raise JSONDecodeError with an Extra data message.

Examples

import json, io
print(json.load(io.StringIO('[1, 2, 3]')))   # → [1, 2, 3]
print(json.load(io.StringIO('{"name": "Bob", "age": 30}')))   # → {'name': 'Bob', 'age': 30}
data = json.load(io.StringIO('{"items": [1, 2, 3]}'))
print(sum(data['items']))   # → 6
print(json.load(io.StringIO('')))   # → JSONDecodeError (пустой ввод)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy