Python Function Reference

json.load

Read JSON from an open file and convert it to a Python object.

Python 3.13
json.load(fp)

Parameters

fpTextIO
A file open for reading in text mode, or io.StringIO.

Return value

A Python object: a dict, list, string, number, bool or None.

Examples

Reading settings from a file

Python 3.13
import json

with open('config.json', 'w', encoding='utf-8') as file:
    file.write('{"debug": true, "level": 3}')

with open('config.json', encoding='utf-8') as file:
    config = json.load(file)
print(config['debug'], config['level'])
True 3

Writing and reading with io.StringIO

Python 3.13
import io
import json

buffer = io.StringIO()
json.dump({'scores': [5, 4]}, buffer)
buffer.seek(0)
print(json.load(buffer))
{'scores': [5, 4]}

Details

It reads everything from the current position to the end of the file, which must contain exactly one JSON value, usually an object or an array. Raises json.JSONDecodeError for invalid JSON. If the JSON has already been read into a string, use json.loads.

See also

Where to learn