jsonyx.load¶
- jsonyx.load(fp, *, allow=NOTHING, cache_keys=False, hooks=None, root='.')[source]¶
Deserialize an open JSON file to a Python object.
Changed in version 2.0: Replaced
use_decimalwithhooks.Changed in version 2.2:
Added
cache_keys.Disabled caching keys by default.
- Parameters:
allow (
Container[str], default:NOTHING) – the JSON deviations fromjsonyx.allowcache_keys (
bool, default:False) – re-use the keys of objectshooks (
dict[str, _Hook] |None, default:None) – the hooks used for transforming dataroot (_StrPath, default:
".") – the path to the archive containing this JSON file
- Raises:
RecursionError – if the JSON file is too deeply nested
TruncatedSyntaxError – when failing to decode the file
ValueError – if a number is too big
- Returns:
Any– a Python object
- Example:
Reading from an open file:
>>> import jsonyx as json >>> from io import StringIO >>> io = StringIO('["streaming API"]') >>> json.load(io) ['streaming API']
Reading from a file:
>>> import jsonyx as json >>> from os.path import join >>> from tempfile import TemporaryDirectory >>> with TemporaryDirectory() as tmpdir: ... filename = join(tmpdir, "file.json") ... with open(filename, "w", encoding="utf-8") as fp: ... _ = fp.write('["streaming API"]') ... with open(filename, "rb") as fp: ... json.load(fp) ... ['streaming API']
Tip
Specify
rootto display the zip filename in error messages.Note
The encoding is detected using
jsonyx.detect_encoding().See also
jsonyx.loads()for deserializing from a string.jsonyx.dump()for serializing to an open file.