all Python 3jsonterm
json.JSONDecoder
classes
The JSON→Python decoder class; allows customizing parsing (e.g. object hooks).
Syntax
json.JSONDecoder().decode(s)
For a one-off parse you do not need this class: loads takes the same object_hook, parse_float and friends and builds a decoder for you. An instance earns its keep through raw_decode(s), which parses only the first JSON value and returns (object, end index) — that lets you walk a string holding several concatenated documents, where plain loads would fail with Extra data.
Examples
import json
print(json.JSONDecoder().decode('[1, 2]')) # → [1, 2]
print(json.JSONDecoder().decode('{"a": 1, "b": null}')) # → {'a': 1, 'b': None}
print(json.JSONDecoder(object_hook=lambda d: sum(d.values())).decode('{"a": 1, "b": 2}')) # → 3
print(json.JSONDecoder().raw_decode('[1, 2] tail')) # → ([1, 2], 6)
print(json.JSONDecoder().decode('{oops}')) # → JSONDecodeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy