blob: d65fb3f1c33ec1d5f73a5618416953fa33014588 [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
123
124Basic Usage
125-----------
126
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300127.. function:: dump(obj, fp, *, skipkeys=False, ensure_ascii=True, \
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200128 check_circular=True, allow_nan=True, cls=None, \
129 indent=None, separators=None, default=None, \
130 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000131
132 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200133 :term:`file-like object`) using this :ref:`conversion table
134 <py-to-json-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000135
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300136 If *skipkeys* is true (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000137 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
138 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000139
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000140 The :mod:`json` module always produces :class:`str` objects, not
141 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
142 input.
143
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300144 If *ensure_ascii* is true (the default), the output is guaranteed to
Éric Araujo6f7aa002012-01-16 10:09:20 +0100145 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300146 false, these characters will be output as-is.
Éric Araujo6f7aa002012-01-16 10:09:20 +0100147
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300148 If *check_circular* is false (default: ``True``), then the circular
Christian Heimes90540002008-05-08 14:29:10 +0000149 reference check for container types will be skipped and a circular reference
150 will result in an :exc:`OverflowError` (or worse).
151
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300152 If *allow_nan* is false (default: ``True``), then it will be a
Christian Heimes90540002008-05-08 14:29:10 +0000153 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300154 ``inf``, ``-inf``) in strict compliance of the JSON specification.
155 If *allow_nan* is true, their JavaScript equivalents (``NaN``,
156 ``Infinity``, ``-Infinity``) will be used.
Christian Heimes90540002008-05-08 14:29:10 +0000157
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000158 If *indent* is a non-negative integer or string, then JSON array elements and
159 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400160 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
161 selects the most compact representation. Using a positive integer indent
Petri Lehtinen72c6eef2012-08-27 20:27:30 +0300162 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
R David Murrayd5315482011-04-12 21:09:18 -0400163 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000164
Petri Lehtinen72b14262012-08-28 07:08:44 +0300165 .. versionchanged:: 3.2
166 Allow strings for *indent* in addition to integers.
167
Ezio Melotti10031442012-11-29 00:42:56 +0200168 If specified, *separators* should be an ``(item_separator, key_separator)``
169 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
170 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
171 you should specify ``(',', ':')`` to eliminate whitespace.
172
173 .. versionchanged:: 3.4
174 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000175
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300176 If specified, *default* should be a function that gets called for objects that
177 can't otherwise be serialized. It should return a JSON encodable version of
178 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
179 is raised.
Christian Heimes90540002008-05-08 14:29:10 +0000180
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300181 If *sort_keys* is true (default: ``False``), then the output of
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200182 dictionaries will be sorted by key.
183
Georg Brandl1f01deb2009-01-03 22:47:39 +0000184 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000185 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000186 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000187
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300188 .. versionchanged:: 3.6
189 All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
Christian Heimes90540002008-05-08 14:29:10 +0000190
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300191
192.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200193 check_circular=True, allow_nan=True, cls=None, \
194 indent=None, separators=None, default=None, \
195 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000196
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200197 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
198 table <py-to-json-table>`. The arguments have the same meaning as in
199 :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000200
Ezio Melotti60adf952011-04-15 07:37:00 +0300201 .. note::
202
Georg Brandl340d2692011-04-16 16:54:15 +0200203 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
204 so trying to serialize multiple objects with repeated calls to
205 :func:`dump` using the same *fp* will result in an invalid JSON file.
206
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700207 .. note::
208
209 Keys in key/value pairs of JSON are always of the type :class:`str`. When
210 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy9cbcc2f2013-03-08 19:35:15 -0500211 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700212 into JSON and then back into a dictionary, the dictionary may not equal
213 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
214 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000215
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300216.. 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 +0000217
Anthony Sottilebb6366b2018-06-07 02:58:12 -0700218 Deserialize *fp* (a ``.read()``-supporting :term:`text file` or
219 :term:`binary file` containing a JSON document) to a Python object using
220 this :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000221
Christian Heimes90540002008-05-08 14:29:10 +0000222 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000223 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000224 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrou331624b2012-08-24 19:37:23 +0200225 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
226 class hinting).
Christian Heimes90540002008-05-08 14:29:10 +0000227
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000228 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000229 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000230 return value of *object_pairs_hook* will be used instead of the
INADA Naoki629338f2018-04-03 12:39:47 +0900231 :class:`dict`. This feature can be used to implement custom decoders.
232 If *object_hook* is also defined, the *object_pairs_hook* takes priority.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000233
234 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000235 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000236
Christian Heimes90540002008-05-08 14:29:10 +0000237 *parse_float*, if specified, will be called with the string of every JSON
238 float to be decoded. By default, this is equivalent to ``float(num_str)``.
239 This can be used to use another datatype or parser for JSON floats
240 (e.g. :class:`decimal.Decimal`).
241
242 *parse_int*, if specified, will be called with the string of every JSON int
243 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
244 be used to use another datatype or parser for JSON integers
245 (e.g. :class:`float`).
246
247 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200248 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
249 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000250 are encountered.
251
Hynek Schlawackf54c0602012-05-20 18:32:53 +0200252 .. versionchanged:: 3.1
Hynek Schlawack1203e832012-05-20 12:03:17 +0200253 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
254
Christian Heimes90540002008-05-08 14:29:10 +0000255 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000256 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
257 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000258
Felix Crux60fb9712013-08-12 17:39:51 -0400259 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200260 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000261
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300262 .. versionchanged:: 3.6
263 All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
264
Anthony Sottilebb6366b2018-06-07 02:58:12 -0700265 .. versionchanged:: 3.6
266 *fp* can now be a :term:`binary file`. The input encoding should be
267 UTF-8, UTF-16 or UTF-32.
268
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300269.. function:: loads(s, *, encoding=None, 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 +0000270
Nick Coghlanb1615622016-09-10 20:16:18 +1000271 Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
272 instance containing a JSON document) to a Python object using this
273 :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000274
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000275 The other arguments have the same meaning as in :func:`load`, except
276 *encoding* which is ignored and deprecated.
Christian Heimes90540002008-05-08 14:29:10 +0000277
Felix Cruxb4357992013-08-12 17:39:51 -0400278 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200279 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000280
Raymond Hettingera57a8a32017-01-19 21:39:37 -0800281 .. versionchanged:: 3.6
282 *s* can now be of type :class:`bytes` or :class:`bytearray`. The
283 input encoding should be UTF-8, UTF-16 or UTF-32.
284
285
Antoine Pitrou331624b2012-08-24 19:37:23 +0200286Encoders and Decoders
Christian Heimes90540002008-05-08 14:29:10 +0000287---------------------
288
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300289.. 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 +0000290
291 Simple JSON decoder.
292
293 Performs the following translations in decoding by default:
294
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200295 .. _json-to-py-table:
296
Christian Heimes90540002008-05-08 14:29:10 +0000297 +---------------+-------------------+
298 | JSON | Python |
299 +===============+===================+
300 | object | dict |
301 +---------------+-------------------+
302 | array | list |
303 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000304 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000305 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000306 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000307 +---------------+-------------------+
308 | number (real) | float |
309 +---------------+-------------------+
310 | true | True |
311 +---------------+-------------------+
312 | false | False |
313 +---------------+-------------------+
314 | null | None |
315 +---------------+-------------------+
316
317 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
318 corresponding ``float`` values, which is outside the JSON spec.
319
Christian Heimes90540002008-05-08 14:29:10 +0000320 *object_hook*, if specified, will be called with the result of every JSON
321 object decoded and its return value will be used in place of the given
322 :class:`dict`. This can be used to provide custom deserializations (e.g. to
323 support JSON-RPC class hinting).
324
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000325 *object_pairs_hook*, if specified will be called with the result of every
326 JSON object decoded with an ordered list of pairs. The return value of
327 *object_pairs_hook* will be used instead of the :class:`dict`. This
INADA Naoki629338f2018-04-03 12:39:47 +0900328 feature can be used to implement custom decoders. If *object_hook* is also
329 defined, the *object_pairs_hook* takes priority.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000330
331 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000332 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000333
Christian Heimes90540002008-05-08 14:29:10 +0000334 *parse_float*, if specified, will be called with the string of every JSON
335 float to be decoded. By default, this is equivalent to ``float(num_str)``.
336 This can be used to use another datatype or parser for JSON floats
337 (e.g. :class:`decimal.Decimal`).
338
339 *parse_int*, if specified, will be called with the string of every JSON int
340 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
341 be used to use another datatype or parser for JSON integers
342 (e.g. :class:`float`).
343
344 *parse_constant*, if specified, will be called with one of the following
Serhiy Storchaka022371f2016-11-12 22:47:16 +0200345 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
346 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000347 are encountered.
348
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300349 If *strict* is false (``True`` is the default), then control characters
Georg Brandld4460aa2010-10-15 17:03:02 +0000350 will be allowed inside strings. Control characters in this context are
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200351 those with character codes in the 0--31 range, including ``'\t'`` (tab),
Georg Brandld4460aa2010-10-15 17:03:02 +0000352 ``'\n'``, ``'\r'`` and ``'\0'``.
353
Felix Crux654f0032013-08-12 17:39:51 -0400354 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200355 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000356
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300357 .. versionchanged:: 3.6
358 All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
359
Christian Heimes90540002008-05-08 14:29:10 +0000360 .. method:: decode(s)
361
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000362 Return the Python representation of *s* (a :class:`str` instance
Martin Panterd21e0b52015-10-10 10:36:22 +0000363 containing a JSON document).
Christian Heimes90540002008-05-08 14:29:10 +0000364
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200365 :exc:`JSONDecodeError` will be raised if the given JSON document is not
366 valid.
367
Christian Heimes90540002008-05-08 14:29:10 +0000368 .. method:: raw_decode(s)
369
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000370 Decode a JSON document from *s* (a :class:`str` beginning with a
371 JSON document) and return a 2-tuple of the Python representation
372 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000373
374 This can be used to decode a JSON document from a string that may have
375 extraneous data at the end.
376
377
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300378.. 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 +0000379
380 Extensible JSON encoder for Python data structures.
381
382 Supports the following objects and types by default:
383
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200384 .. _py-to-json-table:
385
Ethan Furmana4998a72013-08-10 13:01:45 -0700386 +----------------------------------------+---------------+
387 | Python | JSON |
388 +========================================+===============+
389 | dict | object |
390 +----------------------------------------+---------------+
391 | list, tuple | array |
392 +----------------------------------------+---------------+
393 | str | string |
394 +----------------------------------------+---------------+
395 | int, float, int- & float-derived Enums | number |
396 +----------------------------------------+---------------+
397 | True | true |
398 +----------------------------------------+---------------+
399 | False | false |
400 +----------------------------------------+---------------+
401 | None | null |
402 +----------------------------------------+---------------+
403
404 .. versionchanged:: 3.4
405 Added support for int- and float-derived Enum classes.
Christian Heimes90540002008-05-08 14:29:10 +0000406
407 To extend this to recognize other objects, subclass and implement a
408 :meth:`default` method with another method that returns a serializable object
409 for ``o`` if possible, otherwise it should call the superclass implementation
410 (to raise :exc:`TypeError`).
411
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300412 If *skipkeys* is false (the default), then it is a :exc:`TypeError` to
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300413 attempt encoding of keys that are not :class:`str`, :class:`int`,
414 :class:`float` or ``None``. If *skipkeys* is true, such items are simply
415 skipped.
Christian Heimes90540002008-05-08 14:29:10 +0000416
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300417 If *ensure_ascii* is true (the default), the output is guaranteed to
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000418 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300419 false, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000420
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300421 If *check_circular* is true (the default), then lists, dicts, and custom
Christian Heimes90540002008-05-08 14:29:10 +0000422 encoded objects will be checked for circular references during encoding to
423 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
424 Otherwise, no such check takes place.
425
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300426 If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and
Christian Heimes90540002008-05-08 14:29:10 +0000427 ``-Infinity`` will be encoded as such. This behavior is not JSON
428 specification compliant, but is consistent with most JavaScript based
429 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
430 such floats.
431
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300432 If *sort_keys* is true (default: ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000433 will be sorted by key; this is useful for regression tests to ensure that
434 JSON serializations can be compared on a day-to-day basis.
435
Petri Lehtinen72b14262012-08-28 07:08:44 +0300436 If *indent* is a non-negative integer or string, then JSON array elements and
437 object members will be pretty-printed with that indent level. An indent level
438 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
439 selects the most compact representation. Using a positive integer indent
440 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
441 that string is used to indent each level.
442
443 .. versionchanged:: 3.2
444 Allow strings for *indent* in addition to integers.
Christian Heimes90540002008-05-08 14:29:10 +0000445
446 If specified, *separators* should be an ``(item_separator, key_separator)``
Ezio Melotti10031442012-11-29 00:42:56 +0200447 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
448 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
449 you should specify ``(',', ':')`` to eliminate whitespace.
450
451 .. versionchanged:: 3.4
452 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000453
Serhiy Storchaka15287f82016-06-30 13:59:12 +0300454 If specified, *default* should be a function that gets called for objects that
455 can't otherwise be serialized. It should return a JSON encodable version of
456 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
457 is raised.
Christian Heimes90540002008-05-08 14:29:10 +0000458
Serhiy Storchakaaacd53f2016-06-22 00:03:20 +0300459 .. versionchanged:: 3.6
460 All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
461
Christian Heimes90540002008-05-08 14:29:10 +0000462
463 .. method:: default(o)
464
465 Implement this method in a subclass such that it returns a serializable
466 object for *o*, or calls the base implementation (to raise a
467 :exc:`TypeError`).
468
469 For example, to support arbitrary iterators, you could implement default
470 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000471
Christian Heimes90540002008-05-08 14:29:10 +0000472 def default(self, o):
473 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000474 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000475 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000476 pass
Christian Heimes90540002008-05-08 14:29:10 +0000477 else:
478 return list(iterable)
R David Murraydd246172013-03-17 21:52:35 -0400479 # Let the base class default method raise the TypeError
Georg Brandl0bb73b82010-09-03 22:36:22 +0000480 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000481
482
483 .. method:: encode(o)
484
485 Return a JSON string representation of a Python data structure, *o*. For
486 example::
487
Georg Brandl0bb73b82010-09-03 22:36:22 +0000488 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000489 '{"foo": ["bar", "baz"]}'
490
491
492 .. method:: iterencode(o)
493
494 Encode the given object, *o*, and yield each string representation as
495 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000496
Georg Brandl0bb73b82010-09-03 22:36:22 +0000497 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000498 mysocket.write(chunk)
Antoine Pitrou331624b2012-08-24 19:37:23 +0200499
500
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200501Exceptions
502----------
503
Serhiy Storchaka5becf382017-05-27 16:11:18 +0300504.. exception:: JSONDecodeError(msg, doc, pos)
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200505
Martin Panteref107ee2017-01-24 00:26:56 +0000506 Subclass of :exc:`ValueError` with the following additional attributes:
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200507
Martin Panteref107ee2017-01-24 00:26:56 +0000508 .. attribute:: msg
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200509
Martin Panteref107ee2017-01-24 00:26:56 +0000510 The unformatted error message.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200511
Martin Panteref107ee2017-01-24 00:26:56 +0000512 .. attribute:: doc
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200513
Martin Panteref107ee2017-01-24 00:26:56 +0000514 The JSON document being parsed.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200515
Martin Panteref107ee2017-01-24 00:26:56 +0000516 .. attribute:: pos
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200517
Martin Panteref107ee2017-01-24 00:26:56 +0000518 The start index of *doc* where parsing failed.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200519
Martin Panteref107ee2017-01-24 00:26:56 +0000520 .. attribute:: lineno
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200521
Martin Panteref107ee2017-01-24 00:26:56 +0000522 The line corresponding to *pos*.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200523
Martin Panteref107ee2017-01-24 00:26:56 +0000524 .. attribute:: colno
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200525
Martin Panteref107ee2017-01-24 00:26:56 +0000526 The column corresponding to *pos*.
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200527
528 .. versionadded:: 3.5
529
530
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200531Standard Compliance and Interoperability
532----------------------------------------
Antoine Pitrou331624b2012-08-24 19:37:23 +0200533
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200534The JSON format is specified by :rfc:`7159` and by
535`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
536This section details this module's level of compliance with the RFC.
537For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
538parameters other than those explicitly mentioned, are not considered.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200539
540This module does not comply with the RFC in a strict fashion, implementing some
541extensions that are valid JavaScript but not valid JSON. In particular:
542
Antoine Pitrou331624b2012-08-24 19:37:23 +0200543- Infinite and NaN number values are accepted and output;
544- Repeated names within an object are accepted, and only the value of the last
545 name-value pair is used.
546
547Since the RFC permits RFC-compliant parsers to accept input texts that are not
548RFC-compliant, this module's deserializer is technically RFC-compliant under
549default settings.
550
551Character Encodings
552^^^^^^^^^^^^^^^^^^^
553
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200554The RFC requires that JSON be represented using either UTF-8, UTF-16, or
555UTF-32, with UTF-8 being the recommended default for maximum interoperability.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200556
557As permitted, though not required, by the RFC, this module's serializer sets
558*ensure_ascii=True* by default, thus escaping the output so that the resulting
559strings only contain ASCII characters.
560
561Other than the *ensure_ascii* parameter, this module is defined strictly in
562terms of conversion between Python objects and
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200563:class:`Unicode strings <str>`, and thus does not otherwise directly address
564the issue of character encodings.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200565
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200566The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
567and this module's serializer does not add a BOM to its output.
568The RFC permits, but does not require, JSON deserializers to ignore an initial
569BOM in their input. This module's deserializer raises a :exc:`ValueError`
570when an initial BOM is present.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200571
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200572The RFC does not explicitly forbid JSON strings which contain byte sequences
573that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
574surrogates), but it does note that they may cause interoperability problems.
575By default, this module accepts and outputs (when present in the original
Serhiy Storchakad3faf432015-01-18 11:28:37 +0200576:class:`str`) code points for such sequences.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200577
578
579Infinite and NaN Number Values
580^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
581
582The RFC does not permit the representation of infinite or NaN number values.
583Despite that, by default, this module accepts and outputs ``Infinity``,
584``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
585
586 >>> # Neither of these calls raises an exception, but the results are not valid JSON
587 >>> json.dumps(float('-inf'))
588 '-Infinity'
589 >>> json.dumps(float('nan'))
590 'NaN'
591 >>> # Same when deserializing
592 >>> json.loads('-Infinity')
593 -inf
594 >>> json.loads('NaN')
595 nan
596
597In the serializer, the *allow_nan* parameter can be used to alter this
598behavior. In the deserializer, the *parse_constant* parameter can be used to
599alter this behavior.
600
601
602Repeated Names Within an Object
603^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
604
605The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200606does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrou331624b2012-08-24 19:37:23 +0200607default, this module does not raise an exception; instead, it ignores all but
608the last name-value pair for a given name::
609
610 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
611 >>> json.loads(weird_json)
612 {'x': 3}
613
614The *object_pairs_hook* parameter can be used to alter this behavior.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500615
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200616
617Top-level Non-Object, Non-Array Values
618^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
619
620The old version of JSON specified by the obsolete :rfc:`4627` required that
621the top-level value of a JSON text must be either a JSON object or array
622(Python :class:`dict` or :class:`list`), and could not be a JSON null,
623boolean, number, or string value. :rfc:`7159` removed that restriction, and
624this module does not and has never implemented that restriction in either its
625serializer or its deserializer.
626
627Regardless, for maximum interoperability, you may wish to voluntarily adhere
628to the restriction yourself.
629
630
631Implementation Limitations
632^^^^^^^^^^^^^^^^^^^^^^^^^^
633
634Some JSON deserializer implementations may set limits on:
635
636* the size of accepted JSON texts
637* the maximum level of nesting of JSON objects and arrays
638* the range and precision of JSON numbers
639* the content and maximum length of JSON strings
640
641This module does not impose any such limits beyond those of the relevant
642Python datatypes themselves or the Python interpreter itself.
643
644When serializing to JSON, beware any such limitations in applications that may
645consume your JSON. In particular, it is common for JSON numbers to be
646deserialized into IEEE 754 double precision numbers and thus subject to that
647representation's range and precision limitations. This is especially relevant
648when serializing Python :class:`int` values of extremely large magnitude, or
649when serializing instances of "exotic" numerical types such as
650:class:`decimal.Decimal`.
651
Benjamin Peterson940e2072014-03-21 23:17:29 -0500652.. _json-commandline:
653
654Command Line Interface
655----------------------
656
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400657.. module:: json.tool
658 :synopsis: A command line to validate and pretty-print JSON.
659
660**Source code:** :source:`Lib/json/tool.py`
661
662--------------
663
Benjamin Peterson940e2072014-03-21 23:17:29 -0500664The :mod:`json.tool` module provides a simple command line interface to validate
665and pretty-print JSON objects.
666
Georg Brandl9e7fbde2014-09-21 00:38:13 +0200667If the optional ``infile`` and ``outfile`` arguments are not
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300668specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively:
669
670.. code-block:: shell-session
Benjamin Peterson940e2072014-03-21 23:17:29 -0500671
672 $ echo '{"json": "obj"}' | python -m json.tool
673 {
674 "json": "obj"
675 }
676 $ echo '{1.2:3.4}' | python -m json.tool
677 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
678
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200679.. versionchanged:: 3.5
680 The output is now in the same order as the input. Use the
681 :option:`--sort-keys` option to sort the output of dictionaries
682 alphabetically by key.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500683
684Command line options
685^^^^^^^^^^^^^^^^^^^^
686
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400687.. cmdoption:: infile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500688
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300689 The JSON file to be validated or pretty-printed:
690
691 .. code-block:: shell-session
Benjamin Peterson940e2072014-03-21 23:17:29 -0500692
693 $ python -m json.tool mp_films.json
694 [
695 {
696 "title": "And Now for Something Completely Different",
697 "year": 1971
698 },
699 {
700 "title": "Monty Python and the Holy Grail",
701 "year": 1975
702 }
703 ]
704
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400705 If *infile* is not specified, read from :attr:`sys.stdin`.
706
707.. cmdoption:: outfile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500708
709 Write the output of the *infile* to the given *outfile*. Otherwise, write it
710 to :attr:`sys.stdout`.
711
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200712.. cmdoption:: --sort-keys
713
714 Sort the output of dictionaries alphabetically by key.
715
716 .. versionadded:: 3.5
717
Benjamin Peterson940e2072014-03-21 23:17:29 -0500718.. cmdoption:: -h, --help
719
720 Show the help message.
Serhiy Storchaka715f01b2014-11-27 19:45:31 +0200721
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200722
723.. rubric:: Footnotes
724
725.. [#rfc-errata] As noted in `the errata for RFC 7159
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300726 <https://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200727 JSON permits literal U+2028 (LINE SEPARATOR) and
728 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
729 (as of ECMAScript Edition 5.1) does not.