On this page
json.load
Prev: json.JSONEncoder | Up: json | Next: json.loads
Rel: json.dump, json.dumps, json.JSONDecodeError, json.JSONDecoder
Ref: docs.python.org — json.load
Tags: #public #python3 #stdlib #json
Import
import json
from json import load
Signature & Overview
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object.
Usage
import io
import json
stream = io.StringIO('{"ok": true, "items": [1, 2, 3]}')
parsed = json.load(stream)
assert parsed == {"ok": True, "items": [1, 2, 3]}
Gotchas
- Reads from a file-like object: Pass an open file handle (
with open(path, encoding="utf-8") as f: json.load(f)); for astrorbytes, usejson.loads(). - Invalid JSON raises
json.JSONDecodeError: CatchJSONDecodeError(a subclass ofValueError) when reading external files. - One JSON value per call: For JSON Lines (
.jsonl/ NDJSON), iterate line-by-line and calljson.loads(line)on each line.
REPL Inspect
from json import load
help(load)
Linked from
Related notes
- tuts/python3/stdlib_module/json/JSONEncoder Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/loads Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/json/dump Linked here; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/dumps Linked here; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONDecodeError Linked here; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONDecoder Linked here; shared tags: #json, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib