blob: e1a246aad6190b7e6cb3f7a664f9f16986e5d3d2 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001:mod:`json` --- JSON encoder and decoder
2========================================
3
4.. module:: json
5 :synopsis: Encode and decode the JSON format.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Christian Heimes90540002008-05-08 14:29:10 +00007.. moduleauthor:: Bob Ippolito <bob@redivi.com>
8.. sectionauthor:: Bob Ippolito <bob@redivi.com>
Christian Heimes90540002008-05-08 14:29:10 +00009
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/json/__init__.py`
11
12--------------
13
Antoine Pitrou331624b2012-08-24 19:37:23 +020014`JSON (JavaScript Object Notation) <http://json.org>`_, specified by
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +020015:rfc:`7159` (which obsoletes :rfc:`4627`) and by
16`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_,
17is a lightweight data interchange format inspired by
Georg Brandl5d941342016-02-26 19:37:12 +010018`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +020019(although it is not a strict subset of JavaScript [#rfc-errata]_ ).
Christian Heimes90540002008-05-08 14:29:10 +000020
21:mod:`json` exposes an API familiar to users of the standard library
22:mod:`marshal` and :mod:`pickle` modules.
23
24Encoding basic Python object hierarchies::
Georg Brandl48310cd2009-01-03 21:18:54 +000025
Christian Heimes90540002008-05-08 14:29:10 +000026 >>> import json
27 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
28 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
Neal Norwitz752abd02008-05-13 04:55:24 +000029 >>> print(json.dumps("\"foo\bar"))
Christian Heimes90540002008-05-08 14:29:10 +000030 "\"foo\bar"
Benjamin Peterson2505bc62008-05-15 02:17:58 +000031 >>> print(json.dumps('\u1234'))
Christian Heimes90540002008-05-08 14:29:10 +000032 "\u1234"
Neal Norwitz752abd02008-05-13 04:55:24 +000033 >>> print(json.dumps('\\'))
Christian Heimes90540002008-05-08 14:29:10 +000034 "\\"
Neal Norwitz752abd02008-05-13 04:55:24 +000035 >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
Christian Heimes90540002008-05-08 14:29:10 +000036 {"a": 0, "b": 0, "c": 0}
Benjamin Peterson2505bc62008-05-15 02:17:58 +000037 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000038 >>> io = StringIO()
39 >>> json.dump(['streaming API'], io)
40 >>> io.getvalue()
41 '["streaming API"]'
42
43Compact encoding::
44
45 >>> import json
Sergey Fedoseeva7fbad92017-09-09 21:39:36 +050046 >>> json.dumps([1, 2, 3, {'4': 5, '6': 7}], separators=(',', ':'))
Christian Heimes90540002008-05-08 14:29:10 +000047 '[1,2,3,{"4":5,"6":7}]'
48
49Pretty printing::
50
51 >>> import json
Neal Norwitz752abd02008-05-13 04:55:24 +000052 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
Christian Heimes90540002008-05-08 14:29:10 +000053 {
Georg Brandl48310cd2009-01-03 21:18:54 +000054 "4": 5,
Christian Heimes90540002008-05-08 14:29:10 +000055 "6": 7
56 }
57
58Decoding JSON::
Georg Brandl48310cd2009-01-03 21:18:54 +000059
Christian Heimes90540002008-05-08 14:29:10 +000060 >>> import json
61 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000062 ['foo', {'bar': ['baz', None, 1.0, 2]}]
Christian Heimes90540002008-05-08 14:29:10 +000063 >>> json.loads('"\\"foo\\bar"')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000064 '"foo\x08ar'
65 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000066 >>> io = StringIO('["streaming API"]')
67 >>> json.load(io)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000068 ['streaming API']
Christian Heimes90540002008-05-08 14:29:10 +000069
70Specializing JSON object decoding::
71
72 >>> import json
73 >>> def as_complex(dct):
74 ... if '__complex__' in dct:
75 ... return complex(dct['real'], dct['imag'])
76 ... return dct
Benjamin Peterson2505bc62008-05-15 02:17:58 +000077 ...
Christian Heimes90540002008-05-08 14:29:10 +000078 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
79 ... object_hook=as_complex)
80 (1+2j)
81 >>> import decimal
82 >>> json.loads('1.1', parse_float=decimal.Decimal)
83 Decimal('1.1')
84
85Extending :class:`JSONEncoder`::
Georg Brandl48310cd2009-01-03 21:18:54 +000086
Christian Heimes90540002008-05-08 14:29:10 +000087 >>> import json
88 >>> class ComplexEncoder(json.JSONEncoder):
89 ... def default(self, obj):
90 ... if isinstance(obj, complex):
91 ... return [obj.real, obj.imag]
R David Murraydd246172013-03-17 21:52:35 -040092 ... # Let the base class default method raise the TypeError
Christian Heimes90540002008-05-08 14:29:10 +000093 ... return json.JSONEncoder.default(self, obj)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000094 ...
Georg Brandl0bb73b82010-09-03 22:36:22 +000095 >>> json.dumps(2 + 1j, cls=ComplexEncoder)
Christian Heimes90540002008-05-08 14:29:10 +000096 '[2.0, 1.0]'
97 >>> ComplexEncoder().encode(2 + 1j)
98 '[2.0, 1.0]'
99 >>> list(ComplexEncoder().iterencode(2 + 1j))
Georg Brandl0bb73b82010-09-03 22:36:22 +0000100 ['[2.0', ', 1.0', ']']
Georg Brandl48310cd2009-01-03 21:18:54 +0000101
Christian Heimes90540002008-05-08 14:29:10 +0000102
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300103Using :mod:`json.tool` from the shell to validate and pretty-print:
Christian Heimes90540002008-05-08 14:29:10 +0000104
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300105.. code-block:: shell-session
Georg Brandl48310cd2009-01-03 21:18:54 +0000106
Georg Brandl946faa32014-10-28 22:54:24 +0100107 $ echo '{"json":"obj"}' | python -m json.tool
Christian Heimes90540002008-05-08 14:29:10 +0000108 {
109 "json": "obj"
110 }
Georg Brandl946faa32014-10-28 22:54:24 +0100111 $ echo '{1.2:3.4}' | python -m json.tool
Serhiy Storchakac510a042013-02-21 20:19:16 +0200112 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Christian Heimes90540002008-05-08 14:29:10 +0000113
Benjamin Peterson940e2072014-03-21 23:17:29 -0500114See :ref:`json-commandline` for detailed documentation.
115
Georg Brandl48310cd2009-01-03 21:18:54 +0000116.. note::
Christian Heimes90540002008-05-08 14:29:10 +0000117
Antoine Pitrou331624b2012-08-24 19:37:23 +0200118 JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by
119 this module's default settings (in particular, the default *separators*
120 value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
121 used as a YAML serializer.
Christian Heimes90540002008-05-08 14:29:10 +0000122
Raymond Hettinger657008e2019-08-22 15:14:42 -0700123.. note::
124
125 This module's encoders and decoders preserve input and output order by
126 default. Order is only lost if the underlying containers are unordered.
127
128 Prior to Python 3.7, :class:`dict` was not guaranteed to be ordered, so
129 inputs and outputs were typically scrambled unless
130 :class:`collections.OrderedDict` was specifically requested. Starting
131 with Python 3.7, the regular :class:`dict` became order preserving, so
Andre Delfinod288b292019-08-23 06:58:27 -0300132 it is no longer necessary to specify :class:`collections.OrderedDict` for
Raymond Hettinger657008e2019-08-22 15:14:42 -0700133 JSON generation and parsing.
134
Christian Heimes90540002008-05-08 14:29:10 +0000135
136Basic Usage
137-----------
138
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300139.. function:: dump(obj, fp, *, skipkeys=False, ensure_ascii=True, \
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200140 check_circular=True, allow_nan=True, cls=None, \
141 indent=None, separators=None, default=None, \
142 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000143
144 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200145 :term:`file-like object`) using this :ref:`conversion table
146 <py-to-json-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000147
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300148 If *skipkeys* is true (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000149 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
150 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000151
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000152 The :mod:`json` module always produces :class:`str` objects, not
153 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
154 input.
155
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300156 If *ensure_ascii* is true (the default), the output is guaranteed to
Éric Araujo6f7aa002012-01-16 10:09:20 +0100157 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300158 false, these characters will be output as-is.
Éric Araujo6f7aa002012-01-16 10:09:20 +0100159
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300160 If *check_circular* is false (default: ``True``), then the circular
Christian Heimes90540002008-05-08 14:29:10 +0000161 reference check for container types will be skipped and a circular reference
162 will result in an :exc:`OverflowError` (or worse).
163
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300164 If *allow_nan* is false (default: ``True``), then it will be a
Christian Heimes90540002008-05-08 14:29:10 +0000165 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300166 ``inf``, ``-inf``) in strict compliance of the JSON specification.
167 If *allow_nan* is true, their JavaScript equivalents (``NaN``,
168 ``Infinity``, ``-Infinity``) will be used.
Christian Heimes90540002008-05-08 14:29:10 +0000169
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000170 If *indent* is a non-negative integer or string, then JSON array elements and
171 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400172 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
173 selects the most compact representation. Using a positive integer indent
Petri Lehtinen72c6eef2012-08-27 20:27:30 +0300174 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
R David Murrayd5315482011-04-12 21:09:18 -0400175 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000176
Petri Lehtinen72b14262012-08-28 07:08:44 +0300177 .. versionchanged:: 3.2
178 Allow strings for *indent* in addition to integers.
179
Ezio Melotti10031442012-11-29 00:42:56 +0200180 If specified, *separators* should be an ``(item_separator, key_separator)``
181 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
182 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
183 you should specify ``(',', ':')`` to eliminate whitespace.
184
185 .. versionchanged:: 3.4
186 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000187
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300188 If specified, *default* should be a function that gets called for objects that
189 can't otherwise be serialized. It should return a JSON encodable version of
190 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
191 is raised.
Christian Heimes90540002008-05-08 14:29:10 +0000192
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300193 If *sort_keys* is true (default: ``False``), then the output of
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200194 dictionaries will be sorted by key.
195
Georg Brandl1f01deb2009-01-03 22:47:39 +0000196 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000197 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000198 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000199
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300200 .. versionchanged:: 3.6
201 All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
Christian Heimes90540002008-05-08 14:29:10 +0000202
Evan Allrich9e840842018-08-11 02:34:02 -0500203 .. note::
204
205 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
206 so trying to serialize multiple objects with repeated calls to
207 :func:`dump` using the same *fp* will result in an invalid JSON file.
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300208
209.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200210 check_circular=True, allow_nan=True, cls=None, \
211 indent=None, separators=None, default=None, \
212 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000213
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200214 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
215 table <py-to-json-table>`. The arguments have the same meaning as in
216 :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000217
Ezio Melotti60adf952011-04-15 07:37:00 +0300218 .. note::
219
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700220 Keys in key/value pairs of JSON are always of the type :class:`str`. When
221 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy9cbcc2f2013-03-08 19:35:15 -0500222 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700223 into JSON and then back into a dictionary, the dictionary may not equal
224 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
225 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000226
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300227.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000228
Anthony Sottilebb6366b2018-06-07 02:58:12 -0700229 Deserialize *fp* (a ``.read()``-supporting :term:`text file` or
230 :term:`binary file` containing a JSON document) to a Python object using
231 this :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000232
Christian Heimes90540002008-05-08 14:29:10 +0000233 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000234 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000235 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrou331624b2012-08-24 19:37:23 +0200236 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
237 class hinting).
Christian Heimes90540002008-05-08 14:29:10 +0000238
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000239 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000240 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000241 return value of *object_pairs_hook* will be used instead of the
INADA Naoki629338f2018-04-03 12:39:47 +0900242 :class:`dict`. This feature can be used to implement custom decoders.
243 If *object_hook* is also defined, the *object_pairs_hook* takes priority.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000244
245 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000246 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000247
Christian Heimes90540002008-05-08 14:29:10 +0000248 *parse_float*, if specified, will be called with the string of every JSON
249 float to be decoded. By default, this is equivalent to ``float(num_str)``.
250 This can be used to use another datatype or parser for JSON floats
251 (e.g. :class:`decimal.Decimal`).
252
253 *parse_int*, if specified, will be called with the string of every JSON int
254 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
255 be used to use another datatype or parser for JSON integers
256 (e.g. :class:`float`).
257
258 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200259 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
260 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000261 are encountered.
262
Hynek Schlawackf54c0602012-05-20 18:32:53 +0200263 .. versionchanged:: 3.1
Hynek Schlawack1203e832012-05-20 12:03:17 +0200264 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
265
Christian Heimes90540002008-05-08 14:29:10 +0000266 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000267 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
268 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000269
Felix Crux60fb9712013-08-12 17:39:51 -0400270 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200271 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000272
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300273 .. versionchanged:: 3.6
274 All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
275
Anthony Sottilebb6366b2018-06-07 02:58:12 -0700276 .. versionchanged:: 3.6
277 *fp* can now be a :term:`binary file`. The input encoding should be
278 UTF-8, UTF-16 or UTF-32.
279
Matthias Bussonniera8abe092019-04-09 00:17:25 -0700280.. function:: loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000281
Nick Coghlanb1615622016-09-10 20:16:18 +1000282 Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
283 instance containing a JSON document) to a Python object using this
284 :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000285
Inada Naoki5bbac8c2020-01-22 19:01:24 +0900286 The other arguments have the same meaning as in :func:`load`.
Christian Heimes90540002008-05-08 14:29:10 +0000287
Felix Cruxb4357992013-08-12 17:39:51 -0400288 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200289 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000290
Raymond Hettingera57a8a32017-01-19 21:39:37 -0800291 .. versionchanged:: 3.6
292 *s* can now be of type :class:`bytes` or :class:`bytearray`. The
293 input encoding should be UTF-8, UTF-16 or UTF-32.
294
Inada Naoki5bbac8c2020-01-22 19:01:24 +0900295 .. versionchanged:: 3.9
296 The keyword argument *encoding* has been removed.
297
Raymond Hettingera57a8a32017-01-19 21:39:37 -0800298
Antoine Pitrou331624b2012-08-24 19:37:23 +0200299Encoders and Decoders
Christian Heimes90540002008-05-08 14:29:10 +0000300---------------------
301
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300302.. class:: JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
Christian Heimes90540002008-05-08 14:29:10 +0000303
304 Simple JSON decoder.
305
306 Performs the following translations in decoding by default:
307
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200308 .. _json-to-py-table:
309
Christian Heimes90540002008-05-08 14:29:10 +0000310 +---------------+-------------------+
311 | JSON | Python |
312 +===============+===================+
313 | object | dict |
314 +---------------+-------------------+
315 | array | list |
316 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000317 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000318 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000319 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000320 +---------------+-------------------+
321 | number (real) | float |
322 +---------------+-------------------+
323 | true | True |
324 +---------------+-------------------+
325 | false | False |
326 +---------------+-------------------+
327 | null | None |
328 +---------------+-------------------+
329
330 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
331 corresponding ``float`` values, which is outside the JSON spec.
332
Christian Heimes90540002008-05-08 14:29:10 +0000333 *object_hook*, if specified, will be called with the result of every JSON
334 object decoded and its return value will be used in place of the given
335 :class:`dict`. This can be used to provide custom deserializations (e.g. to
336 support JSON-RPC class hinting).
337
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000338 *object_pairs_hook*, if specified will be called with the result of every
339 JSON object decoded with an ordered list of pairs. The return value of
340 *object_pairs_hook* will be used instead of the :class:`dict`. This
INADA Naoki629338f2018-04-03 12:39:47 +0900341 feature can be used to implement custom decoders. If *object_hook* is also
342 defined, the *object_pairs_hook* takes priority.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000343
344 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000345 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000346
Christian Heimes90540002008-05-08 14:29:10 +0000347 *parse_float*, if specified, will be called with the string of every JSON
348 float to be decoded. By default, this is equivalent to ``float(num_str)``.
349 This can be used to use another datatype or parser for JSON floats
350 (e.g. :class:`decimal.Decimal`).
351
352 *parse_int*, if specified, will be called with the string of every JSON int
353 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
354 be used to use another datatype or parser for JSON integers
355 (e.g. :class:`float`).
356
357 *parse_constant*, if specified, will be called with one of the following
Serhiy Storchaka022371f2016-11-12 22:47:16 +0200358 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
359 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000360 are encountered.
361
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300362 If *strict* is false (``True`` is the default), then control characters
Georg Brandld4460aa2010-10-15 17:03:02 +0000363 will be allowed inside strings. Control characters in this context are
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200364 those with character codes in the 0--31 range, including ``'\t'`` (tab),
Georg Brandld4460aa2010-10-15 17:03:02 +0000365 ``'\n'``, ``'\r'`` and ``'\0'``.
366
Felix Crux654f0032013-08-12 17:39:51 -0400367 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200368 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000369
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300370 .. versionchanged:: 3.6
371 All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
372
Christian Heimes90540002008-05-08 14:29:10 +0000373 .. method:: decode(s)
374
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000375 Return the Python representation of *s* (a :class:`str` instance
Martin Panterd21e0b52015-10-10 10:36:22 +0000376 containing a JSON document).
Christian Heimes90540002008-05-08 14:29:10 +0000377
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200378 :exc:`JSONDecodeError` will be raised if the given JSON document is not
379 valid.
380
Christian Heimes90540002008-05-08 14:29:10 +0000381 .. method:: raw_decode(s)
382
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000383 Decode a JSON document from *s* (a :class:`str` beginning with a
384 JSON document) and return a 2-tuple of the Python representation
385 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000386
387 This can be used to decode a JSON document from a string that may have
388 extraneous data at the end.
389
390
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300391.. class:: JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Christian Heimes90540002008-05-08 14:29:10 +0000392
393 Extensible JSON encoder for Python data structures.
394
395 Supports the following objects and types by default:
396
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200397 .. _py-to-json-table:
398
Ethan Furmana4998a72013-08-10 13:01:45 -0700399 +----------------------------------------+---------------+
400 | Python | JSON |
401 +========================================+===============+
402 | dict | object |
403 +----------------------------------------+---------------+
404 | list, tuple | array |
405 +----------------------------------------+---------------+
406 | str | string |
407 +----------------------------------------+---------------+
408 | int, float, int- & float-derived Enums | number |
409 +----------------------------------------+---------------+
410 | True | true |
411 +----------------------------------------+---------------+
412 | False | false |
413 +----------------------------------------+---------------+
414 | None | null |
415 +----------------------------------------+---------------+
416
417 .. versionchanged:: 3.4
418 Added support for int- and float-derived Enum classes.
Christian Heimes90540002008-05-08 14:29:10 +0000419
420 To extend this to recognize other objects, subclass and implement a
421 :meth:`default` method with another method that returns a serializable object
422 for ``o`` if possible, otherwise it should call the superclass implementation
423 (to raise :exc:`TypeError`).
424
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300425 If *skipkeys* is false (the default), then it is a :exc:`TypeError` to
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300426 attempt encoding of keys that are not :class:`str`, :class:`int`,
427 :class:`float` or ``None``. If *skipkeys* is true, such items are simply
428 skipped.
Christian Heimes90540002008-05-08 14:29:10 +0000429
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300430 If *ensure_ascii* is true (the default), the output is guaranteed to
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000431 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300432 false, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000433
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300434 If *check_circular* is true (the default), then lists, dicts, and custom
Christian Heimes90540002008-05-08 14:29:10 +0000435 encoded objects will be checked for circular references during encoding to
436 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
437 Otherwise, no such check takes place.
438
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300439 If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and
Christian Heimes90540002008-05-08 14:29:10 +0000440 ``-Infinity`` will be encoded as such. This behavior is not JSON
441 specification compliant, but is consistent with most JavaScript based
442 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
443 such floats.
444
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300445 If *sort_keys* is true (default: ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000446 will be sorted by key; this is useful for regression tests to ensure that
447 JSON serializations can be compared on a day-to-day basis.
448
Petri Lehtinen72b14262012-08-28 07:08:44 +0300449 If *indent* is a non-negative integer or string, then JSON array elements and
450 object members will be pretty-printed with that indent level. An indent level
451 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
452 selects the most compact representation. Using a positive integer indent
453 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
454 that string is used to indent each level.
455
456 .. versionchanged:: 3.2
457 Allow strings for *indent* in addition to integers.
Christian Heimes90540002008-05-08 14:29:10 +0000458
459 If specified, *separators* should be an ``(item_separator, key_separator)``
Ezio Melotti10031442012-11-29 00:42:56 +0200460 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
461 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
462 you should specify ``(',', ':')`` to eliminate whitespace.
463
464 .. versionchanged:: 3.4
465 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000466
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300467 If specified, *default* should be a function that gets called for objects that
468 can't otherwise be serialized. It should return a JSON encodable version of
469 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
470 is raised.
Christian Heimes90540002008-05-08 14:29:10 +0000471
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300472 .. versionchanged:: 3.6
473 All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
474
Christian Heimes90540002008-05-08 14:29:10 +0000475
476 .. method:: default(o)
477
478 Implement this method in a subclass such that it returns a serializable
479 object for *o*, or calls the base implementation (to raise a
480 :exc:`TypeError`).
481
482 For example, to support arbitrary iterators, you could implement default
483 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000484
Christian Heimes90540002008-05-08 14:29:10 +0000485 def default(self, o):
486 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000487 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000488 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000489 pass
Christian Heimes90540002008-05-08 14:29:10 +0000490 else:
491 return list(iterable)
R David Murraydd246172013-03-17 21:52:35 -0400492 # Let the base class default method raise the TypeError
Georg Brandl0bb73b82010-09-03 22:36:22 +0000493 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000494
495
496 .. method:: encode(o)
497
498 Return a JSON string representation of a Python data structure, *o*. For
499 example::
500
Georg Brandl0bb73b82010-09-03 22:36:22 +0000501 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000502 '{"foo": ["bar", "baz"]}'
503
504
505 .. method:: iterencode(o)
506
507 Encode the given object, *o*, and yield each string representation as
508 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000509
Georg Brandl0bb73b82010-09-03 22:36:22 +0000510 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000511 mysocket.write(chunk)
Antoine Pitrou331624b2012-08-24 19:37:23 +0200512
513
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200514Exceptions
515----------
516
Serhiy Storchaka5becf382017-05-27 16:11:18 +0300517.. exception:: JSONDecodeError(msg, doc, pos)
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200518
Martin Panteref107ee2017-01-24 00:26:56 +0000519 Subclass of :exc:`ValueError` with the following additional attributes:
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200520
Martin Panteref107ee2017-01-24 00:26:56 +0000521 .. attribute:: msg
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200522
Martin Panteref107ee2017-01-24 00:26:56 +0000523 The unformatted error message.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200524
Martin Panteref107ee2017-01-24 00:26:56 +0000525 .. attribute:: doc
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200526
Martin Panteref107ee2017-01-24 00:26:56 +0000527 The JSON document being parsed.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200528
Martin Panteref107ee2017-01-24 00:26:56 +0000529 .. attribute:: pos
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200530
Martin Panteref107ee2017-01-24 00:26:56 +0000531 The start index of *doc* where parsing failed.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200532
Martin Panteref107ee2017-01-24 00:26:56 +0000533 .. attribute:: lineno
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200534
Martin Panteref107ee2017-01-24 00:26:56 +0000535 The line corresponding to *pos*.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200536
Martin Panteref107ee2017-01-24 00:26:56 +0000537 .. attribute:: colno
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200538
Martin Panteref107ee2017-01-24 00:26:56 +0000539 The column corresponding to *pos*.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200540
541 .. versionadded:: 3.5
542
543
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200544Standard Compliance and Interoperability
545----------------------------------------
Antoine Pitrou331624b2012-08-24 19:37:23 +0200546
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200547The JSON format is specified by :rfc:`7159` and by
548`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
549This section details this module's level of compliance with the RFC.
550For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
551parameters other than those explicitly mentioned, are not considered.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200552
553This module does not comply with the RFC in a strict fashion, implementing some
554extensions that are valid JavaScript but not valid JSON. In particular:
555
Antoine Pitrou331624b2012-08-24 19:37:23 +0200556- Infinite and NaN number values are accepted and output;
557- Repeated names within an object are accepted, and only the value of the last
558 name-value pair is used.
559
560Since the RFC permits RFC-compliant parsers to accept input texts that are not
561RFC-compliant, this module's deserializer is technically RFC-compliant under
562default settings.
563
564Character Encodings
565^^^^^^^^^^^^^^^^^^^
566
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200567The RFC requires that JSON be represented using either UTF-8, UTF-16, or
568UTF-32, with UTF-8 being the recommended default for maximum interoperability.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200569
570As permitted, though not required, by the RFC, this module's serializer sets
571*ensure_ascii=True* by default, thus escaping the output so that the resulting
572strings only contain ASCII characters.
573
574Other than the *ensure_ascii* parameter, this module is defined strictly in
575terms of conversion between Python objects and
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200576:class:`Unicode strings <str>`, and thus does not otherwise directly address
577the issue of character encodings.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200578
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200579The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
580and this module's serializer does not add a BOM to its output.
581The RFC permits, but does not require, JSON deserializers to ignore an initial
582BOM in their input. This module's deserializer raises a :exc:`ValueError`
583when an initial BOM is present.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200584
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200585The RFC does not explicitly forbid JSON strings which contain byte sequences
586that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
587surrogates), but it does note that they may cause interoperability problems.
588By default, this module accepts and outputs (when present in the original
Serhiy Storchakad3faf432015-01-18 11:28:37 +0200589:class:`str`) code points for such sequences.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200590
591
592Infinite and NaN Number Values
593^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
594
595The RFC does not permit the representation of infinite or NaN number values.
596Despite that, by default, this module accepts and outputs ``Infinity``,
597``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
598
599 >>> # Neither of these calls raises an exception, but the results are not valid JSON
600 >>> json.dumps(float('-inf'))
601 '-Infinity'
602 >>> json.dumps(float('nan'))
603 'NaN'
604 >>> # Same when deserializing
605 >>> json.loads('-Infinity')
606 -inf
607 >>> json.loads('NaN')
608 nan
609
610In the serializer, the *allow_nan* parameter can be used to alter this
611behavior. In the deserializer, the *parse_constant* parameter can be used to
612alter this behavior.
613
614
615Repeated Names Within an Object
616^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
617
618The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200619does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrou331624b2012-08-24 19:37:23 +0200620default, this module does not raise an exception; instead, it ignores all but
621the last name-value pair for a given name::
622
623 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
624 >>> json.loads(weird_json)
625 {'x': 3}
626
627The *object_pairs_hook* parameter can be used to alter this behavior.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500628
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200629
630Top-level Non-Object, Non-Array Values
631^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
632
633The old version of JSON specified by the obsolete :rfc:`4627` required that
634the top-level value of a JSON text must be either a JSON object or array
635(Python :class:`dict` or :class:`list`), and could not be a JSON null,
636boolean, number, or string value. :rfc:`7159` removed that restriction, and
637this module does not and has never implemented that restriction in either its
638serializer or its deserializer.
639
640Regardless, for maximum interoperability, you may wish to voluntarily adhere
641to the restriction yourself.
642
643
644Implementation Limitations
645^^^^^^^^^^^^^^^^^^^^^^^^^^
646
647Some JSON deserializer implementations may set limits on:
648
649* the size of accepted JSON texts
650* the maximum level of nesting of JSON objects and arrays
651* the range and precision of JSON numbers
652* the content and maximum length of JSON strings
653
654This module does not impose any such limits beyond those of the relevant
655Python datatypes themselves or the Python interpreter itself.
656
657When serializing to JSON, beware any such limitations in applications that may
658consume your JSON. In particular, it is common for JSON numbers to be
659deserialized into IEEE 754 double precision numbers and thus subject to that
660representation's range and precision limitations. This is especially relevant
661when serializing Python :class:`int` values of extremely large magnitude, or
662when serializing instances of "exotic" numerical types such as
663:class:`decimal.Decimal`.
664
Serhiy Storchaka083a7a12018-11-05 17:47:27 +0200665
Benjamin Peterson940e2072014-03-21 23:17:29 -0500666.. _json-commandline:
Serhiy Storchaka083a7a12018-11-05 17:47:27 +0200667.. program:: json.tool
Benjamin Peterson940e2072014-03-21 23:17:29 -0500668
669Command Line Interface
670----------------------
671
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400672.. module:: json.tool
673 :synopsis: A command line to validate and pretty-print JSON.
674
675**Source code:** :source:`Lib/json/tool.py`
676
677--------------
678
Benjamin Peterson940e2072014-03-21 23:17:29 -0500679The :mod:`json.tool` module provides a simple command line interface to validate
680and pretty-print JSON objects.
681
Georg Brandl9e7fbde2014-09-21 00:38:13 +0200682If the optional ``infile`` and ``outfile`` arguments are not
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300683specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively:
684
685.. code-block:: shell-session
Benjamin Peterson940e2072014-03-21 23:17:29 -0500686
687 $ echo '{"json": "obj"}' | python -m json.tool
688 {
689 "json": "obj"
690 }
691 $ echo '{1.2:3.4}' | python -m json.tool
692 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
693
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200694.. versionchanged:: 3.5
695 The output is now in the same order as the input. Use the
696 :option:`--sort-keys` option to sort the output of dictionaries
697 alphabetically by key.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500698
Serhiy Storchaka083a7a12018-11-05 17:47:27 +0200699
Benjamin Peterson940e2072014-03-21 23:17:29 -0500700Command line options
701^^^^^^^^^^^^^^^^^^^^
702
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400703.. cmdoption:: infile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500704
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300705 The JSON file to be validated or pretty-printed:
706
707 .. code-block:: shell-session
Benjamin Peterson940e2072014-03-21 23:17:29 -0500708
709 $ python -m json.tool mp_films.json
710 [
711 {
712 "title": "And Now for Something Completely Different",
713 "year": 1971
714 },
715 {
716 "title": "Monty Python and the Holy Grail",
717 "year": 1975
718 }
719 ]
720
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400721 If *infile* is not specified, read from :attr:`sys.stdin`.
722
723.. cmdoption:: outfile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500724
725 Write the output of the *infile* to the given *outfile*. Otherwise, write it
726 to :attr:`sys.stdout`.
727
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200728.. cmdoption:: --sort-keys
729
730 Sort the output of dictionaries alphabetically by key.
731
732 .. versionadded:: 3.5
733
wim glennefefe252019-12-06 00:44:01 -0600734.. cmdoption:: --no-ensure-ascii
735
736 Disable escaping of non-ascii characters, see :func:`json.dumps` for more information.
737
738 .. versionadded:: 3.9
739
HongWeipengf1944792018-11-07 18:09:32 +0800740.. cmdoption:: --json-lines
741
742 Parse every input line as separate JSON object.
743
744 .. versionadded:: 3.8
745
Daniel Himmelstein15fb7fa2019-12-07 07:14:40 -0700746.. cmdoption:: --indent, --tab, --no-indent, --compact
747
Mathieu Dupuy12695f42020-08-20 22:08:37 +0200748 Mutually exclusive options for whitespace control.
Daniel Himmelstein15fb7fa2019-12-07 07:14:40 -0700749
750 .. versionadded:: 3.9
751
Benjamin Peterson940e2072014-03-21 23:17:29 -0500752.. cmdoption:: -h, --help
753
754 Show the help message.
Serhiy Storchaka715f01b2014-11-27 19:45:31 +0200755
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200756
757.. rubric:: Footnotes
758
759.. [#rfc-errata] As noted in `the errata for RFC 7159
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300760 <https://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200761 JSON permits literal U+2028 (LINE SEPARATOR) and
762 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
763 (as of ECMAScript Edition 5.1) does not.