On this page
json.JSONDecoder
Prev: json.JSONDecodeError | Up: json | Next: json.JSONEncoder
Rel: json.dump, json.dumps, json.JSONDecodeError, json.JSONEncoder
Ref: docs.python.org โ json.JSONDecoder
Tags: #public #python3 #stdlib #json
Import
import json
from json import JSONDecoder
Signature & Overview
json.JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
Simple JSON https://json.org decoder Performs the following translations in decoding by default: +---------------+-------------------+
Methods & Attributes
JSONDecoder.decode(self, s, _w=<built-in method match of re.Pattern object at 0x109c09f20>)โ Return the Python representation ofs(astrinstance containing a JSON document).JSONDecoder.raw_decode(self, s, idx=0)โ Decode a JSON document froms(astrbeginning with a JSON document) and return a 2-tuple of the Python representation and the index inswhere the document ended.
Usage
import json
# raw_decode() parses one JSON value from a buffer and returns (value, next_index)
decoder = json.JSONDecoder()
buf = '{"a": 1} {"b": 2}'
first, idx = decoder.raw_decode(buf)
second, _ = decoder.raw_decode(buf, idx + 1)
assert first == {"a": 1} and second == {"b": 2}
Gotchas
- Reach for
JSONDecoderwhen streaming concatenated JSON:raw_decode(s, idx)lets you parse back-to-back JSON objects without splitting on newlines. - For simple hooks,
json.loads()is enough: You don't need to subclassJSONDecoderjust to passobject_hook=orparse_float=.
REPL Inspect
from json import JSONDecoder
help(JSONDecoder)
Linked from
Related notes
- tuts/python3/stdlib_module/json/dump Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/dumps Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONDecodeError Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONEncoder 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/load links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/loads links to this page; 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