datalad_next.itertools.load_json
- datalad_next.itertools.load_json(iterable: Iterable[bytes | str]) Generator[Any, None, None] [source]
Convert items yielded by
iterable
into JSON objects and yield themThis function fetches items from the underlying iterable. The items are expected to be
bytes
,str
, orbytearry
, and contain one JSON-encoded object. Items are converted into a JSON-object, by feeding them intojson.loads
.On successful conversion to a JSON-object,
load_json
will yield the resulting JSON-object. If the conversion to a JSON-object fails,load_json
will raise ajson.decoder.JSONDecodeError
:>>> from datalad_next.itertools import load_json, load_json_with_flag >>> tuple(load_json(['{"a": 1}'])) ({'a': 1},) >>> tuple(load_json(['{"c": 3'])) # Faulty JSON-encoding, doctest: +SKIP Traceback (most recent call last): ... json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7)
Using
load_json
together withitemize
allows the processing of JSON-lines data.itemize
will yield a single item for each line andload_json
will convert it into a JSON-object.Note: JSON-decoding is slightly faster if the items of type
str
. Items of typebytes
orbytearray
will work as well, but processing might be slower.- Parameters:
iterable (Iterable[bytes | str]) -- The iterable that yields the JSON-strings or -bytestrings that should be parsed and converted into JSON-objects
- Yields:
Any -- The JSON-object that are generated from the data yielded by
iterable
- Raises:
json.decoder.JSONDecodeError -- If the data yielded by
iterable
is not a valid JSON-string