PyDocs

Nav

Internet Data Handling

load

json

function (source)
  • load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, array_hook=None, **kw)
Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object.

``object_hook`` is an optional function that will be called with the
result of any object literal decode (a ``dict``). The return value of
``object_hook`` will be used instead of the ``dict``. This feature
can be used to implement custom decoders (e.g. JSON-RPC class hinting).

``object_pairs_hook`` is an optional function that will be called with
the result of any object literal decoded with an ordered list of pairs.
The return value of ``object_pairs_hook`` will be used instead of the
``dict``.  This feature can be used to implement custom decoders.  If
``object_hook`` is also defined, the ``object_pairs_hook`` takes
priority.

``array_hook`` is an optional function that will be called with the
result of any literal array decode (a ``list``).  The return value of
this function will be used instead of the ``list``.  This feature can
be used along ``object_pairs_hook`` to customize the resulting data
structure - for example, by setting that to ``frozendict`` and
``array_hook`` to ``tuple``, one can get a deep immutable data structure
from any JSON data.

To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg; otherwise ``JSONDecoder`` is used.
Changed in 3.1, 3.6, 3.11, 3.15
  • 3.1: Added the optional object_pairs_hook parameter. parse_constant doesn’t get called on ‘null’, ‘true’, ‘false’ anymore.
  • 3.6: All optional parameters are now keyword-only. fp can now be a binary file. The input encoding should be UTF-8, UTF-16 or UTF-32.
  • 3.11: The default parse_int of int() now limits the maximum length of the integer string via the interpreter’s integer string conversion length limitation to help avoid denial of service attacks.
  • 3.15: Added the optional array_hook parameter.
1 Example
>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('"\\"foo\\bar"')
'"foo\x08ar'
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)
['streaming API']
from the Python docs
See Also
No see-alsos for json/load
0 Notes
No notes for load