blob: 174e73411511595fe2ef1ab19399d1492db02243 [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
Éric Araujode579d42011-04-21 02:37:41 +020046 >>> 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
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600103.. highlight:: bash
Christian Heimes90540002008-05-08 14:29:10 +0000104
105Using json.tool from the shell to validate and pretty-print::
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
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600116.. highlight:: python3
Christian Heimes90540002008-05-08 14:29:10 +0000117
Georg Brandl48310cd2009-01-03 21:18:54 +0000118.. note::
Christian Heimes90540002008-05-08 14:29:10 +0000119
Antoine Pitrou331624b2012-08-24 19:37:23 +0200120 JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by
121 this module's default settings (in particular, the default *separators*
122 value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
123 used as a YAML serializer.
Christian Heimes90540002008-05-08 14:29:10 +0000124
125
126Basic Usage
127-----------
128
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200129.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
130 check_circular=True, allow_nan=True, cls=None, \
131 indent=None, separators=None, default=None, \
132 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000133
134 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200135 :term:`file-like object`) using this :ref:`conversion table
136 <py-to-json-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000137
138 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000139 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
140 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000141
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000142 The :mod:`json` module always produces :class:`str` objects, not
143 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
144 input.
145
Éric Araujo6f7aa002012-01-16 10:09:20 +0100146 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
147 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
148 ``False``, these characters will be output as-is.
149
Christian Heimes90540002008-05-08 14:29:10 +0000150 If *check_circular* is ``False`` (default: ``True``), then the circular
151 reference check for container types will be skipped and a circular reference
152 will result in an :exc:`OverflowError` (or worse).
153
154 If *allow_nan* is ``False`` (default: ``True``), then it will be a
155 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
156 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
157 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
158
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000159 If *indent* is a non-negative integer or string, then JSON array elements and
160 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400161 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
162 selects the most compact representation. Using a positive integer indent
Petri Lehtinen72c6eef2012-08-27 20:27:30 +0300163 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
R David Murrayd5315482011-04-12 21:09:18 -0400164 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000165
Petri Lehtinen72b14262012-08-28 07:08:44 +0300166 .. versionchanged:: 3.2
167 Allow strings for *indent* in addition to integers.
168
Ezio Melotti10031442012-11-29 00:42:56 +0200169 If specified, *separators* should be an ``(item_separator, key_separator)``
170 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
171 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
172 you should specify ``(',', ':')`` to eliminate whitespace.
173
174 .. versionchanged:: 3.4
175 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000176
Christian Heimes90540002008-05-08 14:29:10 +0000177 *default(obj)* is a function that should return a serializable version of
178 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
179
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200180 If *sort_keys* is ``True`` (default: ``False``), then the output of
181 dictionaries will be sorted by key.
182
Georg Brandl1f01deb2009-01-03 22:47:39 +0000183 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000184 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000185 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000186
187
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200188.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
189 check_circular=True, allow_nan=True, cls=None, \
190 indent=None, separators=None, default=None, \
191 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000192
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200193 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
194 table <py-to-json-table>`. The arguments have the same meaning as in
195 :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000196
Ezio Melotti60adf952011-04-15 07:37:00 +0300197 .. note::
198
Georg Brandl340d2692011-04-16 16:54:15 +0200199 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
200 so trying to serialize multiple objects with repeated calls to
201 :func:`dump` using the same *fp* will result in an invalid JSON file.
202
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700203 .. note::
204
205 Keys in key/value pairs of JSON are always of the type :class:`str`. When
206 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy9cbcc2f2013-03-08 19:35:15 -0500207 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700208 into JSON and then back into a dictionary, the dictionary may not equal
209 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
210 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000211
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000212.. 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 +0000213
Antoine Pitrou15251a92012-08-24 19:49:08 +0200214 Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200215 containing a JSON document) to a Python object using this :ref:`conversion
216 table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000217
Christian Heimes90540002008-05-08 14:29:10 +0000218 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000219 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000220 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrou331624b2012-08-24 19:37:23 +0200221 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
222 class hinting).
Christian Heimes90540002008-05-08 14:29:10 +0000223
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000224 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000225 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000226 return value of *object_pairs_hook* will be used instead of the
227 :class:`dict`. This feature can be used to implement custom decoders that
228 rely on the order that the key and value pairs are decoded (for example,
229 :func:`collections.OrderedDict` will remember the order of insertion). If
230 *object_hook* is also defined, the *object_pairs_hook* takes priority.
231
232 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000233 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000234
Christian Heimes90540002008-05-08 14:29:10 +0000235 *parse_float*, if specified, will be called with the string of every JSON
236 float to be decoded. By default, this is equivalent to ``float(num_str)``.
237 This can be used to use another datatype or parser for JSON floats
238 (e.g. :class:`decimal.Decimal`).
239
240 *parse_int*, if specified, will be called with the string of every JSON int
241 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
242 be used to use another datatype or parser for JSON integers
243 (e.g. :class:`float`).
244
245 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200246 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
247 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000248 are encountered.
249
Hynek Schlawackf54c0602012-05-20 18:32:53 +0200250 .. versionchanged:: 3.1
Hynek Schlawack1203e832012-05-20 12:03:17 +0200251 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
252
Christian Heimes90540002008-05-08 14:29:10 +0000253 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000254 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
255 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000256
Felix Crux60fb9712013-08-12 17:39:51 -0400257 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200258 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000259
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000260.. 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 +0000261
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000262 Deserialize *s* (a :class:`str` instance containing a JSON document) to a
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200263 Python object using this :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000264
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000265 The other arguments have the same meaning as in :func:`load`, except
266 *encoding* which is ignored and deprecated.
Christian Heimes90540002008-05-08 14:29:10 +0000267
Felix Cruxb4357992013-08-12 17:39:51 -0400268 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200269 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000270
Antoine Pitrou331624b2012-08-24 19:37:23 +0200271Encoders and Decoders
Christian Heimes90540002008-05-08 14:29:10 +0000272---------------------
273
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000274.. 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 +0000275
276 Simple JSON decoder.
277
278 Performs the following translations in decoding by default:
279
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200280 .. _json-to-py-table:
281
Christian Heimes90540002008-05-08 14:29:10 +0000282 +---------------+-------------------+
283 | JSON | Python |
284 +===============+===================+
285 | object | dict |
286 +---------------+-------------------+
287 | array | list |
288 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000289 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000290 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000291 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000292 +---------------+-------------------+
293 | number (real) | float |
294 +---------------+-------------------+
295 | true | True |
296 +---------------+-------------------+
297 | false | False |
298 +---------------+-------------------+
299 | null | None |
300 +---------------+-------------------+
301
302 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
303 corresponding ``float`` values, which is outside the JSON spec.
304
Christian Heimes90540002008-05-08 14:29:10 +0000305 *object_hook*, if specified, will be called with the result of every JSON
306 object decoded and its return value will be used in place of the given
307 :class:`dict`. This can be used to provide custom deserializations (e.g. to
308 support JSON-RPC class hinting).
309
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000310 *object_pairs_hook*, if specified will be called with the result of every
311 JSON object decoded with an ordered list of pairs. The return value of
312 *object_pairs_hook* will be used instead of the :class:`dict`. This
313 feature can be used to implement custom decoders that rely on the order
314 that the key and value pairs are decoded (for example,
315 :func:`collections.OrderedDict` will remember the order of insertion). If
316 *object_hook* is also defined, the *object_pairs_hook* takes priority.
317
318 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000319 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000320
Christian Heimes90540002008-05-08 14:29:10 +0000321 *parse_float*, if specified, will be called with the string of every JSON
322 float to be decoded. By default, this is equivalent to ``float(num_str)``.
323 This can be used to use another datatype or parser for JSON floats
324 (e.g. :class:`decimal.Decimal`).
325
326 *parse_int*, if specified, will be called with the string of every JSON int
327 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
328 be used to use another datatype or parser for JSON integers
329 (e.g. :class:`float`).
330
331 *parse_constant*, if specified, will be called with one of the following
332 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
333 ``'false'``. This can be used to raise an exception if invalid JSON numbers
334 are encountered.
335
Georg Brandld4460aa2010-10-15 17:03:02 +0000336 If *strict* is ``False`` (``True`` is the default), then control characters
337 will be allowed inside strings. Control characters in this context are
338 those with character codes in the 0-31 range, including ``'\t'`` (tab),
339 ``'\n'``, ``'\r'`` and ``'\0'``.
340
Felix Crux654f0032013-08-12 17:39:51 -0400341 If the data being deserialized is not a valid JSON document, a
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200342 :exc:`JSONDecodeError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000343
344 .. method:: decode(s)
345
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000346 Return the Python representation of *s* (a :class:`str` instance
Martin Panterd21e0b52015-10-10 10:36:22 +0000347 containing a JSON document).
Christian Heimes90540002008-05-08 14:29:10 +0000348
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200349 :exc:`JSONDecodeError` will be raised if the given JSON document is not
350 valid.
351
Christian Heimes90540002008-05-08 14:29:10 +0000352 .. method:: raw_decode(s)
353
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000354 Decode a JSON document from *s* (a :class:`str` beginning with a
355 JSON document) and return a 2-tuple of the Python representation
356 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000357
358 This can be used to decode a JSON document from a string that may have
359 extraneous data at the end.
360
361
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000362.. 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 +0000363
364 Extensible JSON encoder for Python data structures.
365
366 Supports the following objects and types by default:
367
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200368 .. _py-to-json-table:
369
Ethan Furmana4998a72013-08-10 13:01:45 -0700370 +----------------------------------------+---------------+
371 | Python | JSON |
372 +========================================+===============+
373 | dict | object |
374 +----------------------------------------+---------------+
375 | list, tuple | array |
376 +----------------------------------------+---------------+
377 | str | string |
378 +----------------------------------------+---------------+
379 | int, float, int- & float-derived Enums | number |
380 +----------------------------------------+---------------+
381 | True | true |
382 +----------------------------------------+---------------+
383 | False | false |
384 +----------------------------------------+---------------+
385 | None | null |
386 +----------------------------------------+---------------+
387
388 .. versionchanged:: 3.4
389 Added support for int- and float-derived Enum classes.
Christian Heimes90540002008-05-08 14:29:10 +0000390
391 To extend this to recognize other objects, subclass and implement a
392 :meth:`default` method with another method that returns a serializable object
393 for ``o`` if possible, otherwise it should call the superclass implementation
394 (to raise :exc:`TypeError`).
395
396 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
Georg Brandl639ce962009-04-11 18:18:16 +0000397 attempt encoding of keys that are not str, int, float or None. If
Christian Heimes90540002008-05-08 14:29:10 +0000398 *skipkeys* is ``True``, such items are simply skipped.
399
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000400 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
401 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
402 ``False``, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000403
404 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
405 encoded objects will be checked for circular references during encoding to
406 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
407 Otherwise, no such check takes place.
408
409 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
410 ``-Infinity`` will be encoded as such. This behavior is not JSON
411 specification compliant, but is consistent with most JavaScript based
412 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
413 such floats.
414
Georg Brandl6a74da32010-08-22 20:23:38 +0000415 If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000416 will be sorted by key; this is useful for regression tests to ensure that
417 JSON serializations can be compared on a day-to-day basis.
418
Petri Lehtinen72b14262012-08-28 07:08:44 +0300419 If *indent* is a non-negative integer or string, then JSON array elements and
420 object members will be pretty-printed with that indent level. An indent level
421 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
422 selects the most compact representation. Using a positive integer indent
423 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
424 that string is used to indent each level.
425
426 .. versionchanged:: 3.2
427 Allow strings for *indent* in addition to integers.
Christian Heimes90540002008-05-08 14:29:10 +0000428
429 If specified, *separators* should be an ``(item_separator, key_separator)``
Ezio Melotti10031442012-11-29 00:42:56 +0200430 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
431 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
432 you should specify ``(',', ':')`` to eliminate whitespace.
433
434 .. versionchanged:: 3.4
435 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000436
437 If specified, *default* is a function that gets called for objects that can't
438 otherwise be serialized. It should return a JSON encodable version of the
439 object or raise a :exc:`TypeError`.
440
Christian Heimes90540002008-05-08 14:29:10 +0000441
442 .. method:: default(o)
443
444 Implement this method in a subclass such that it returns a serializable
445 object for *o*, or calls the base implementation (to raise a
446 :exc:`TypeError`).
447
448 For example, to support arbitrary iterators, you could implement default
449 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000450
Christian Heimes90540002008-05-08 14:29:10 +0000451 def default(self, o):
452 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000453 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000454 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000455 pass
Christian Heimes90540002008-05-08 14:29:10 +0000456 else:
457 return list(iterable)
R David Murraydd246172013-03-17 21:52:35 -0400458 # Let the base class default method raise the TypeError
Georg Brandl0bb73b82010-09-03 22:36:22 +0000459 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000460
461
462 .. method:: encode(o)
463
464 Return a JSON string representation of a Python data structure, *o*. For
465 example::
466
Georg Brandl0bb73b82010-09-03 22:36:22 +0000467 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000468 '{"foo": ["bar", "baz"]}'
469
470
471 .. method:: iterencode(o)
472
473 Encode the given object, *o*, and yield each string representation as
474 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000475
Georg Brandl0bb73b82010-09-03 22:36:22 +0000476 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000477 mysocket.write(chunk)
Antoine Pitrou331624b2012-08-24 19:37:23 +0200478
479
Serhiy Storchaka47efb4a2015-01-26 13:16:30 +0200480Exceptions
481----------
482
483.. exception:: JSONDecodeError(msg, doc, pos, end=None)
484
485 Subclass of :exc:`ValueError` with the following additional attributes:
486
487 .. attribute:: msg
488
489 The unformatted error message.
490
491 .. attribute:: doc
492
493 The JSON document being parsed.
494
495 .. attribute:: pos
496
497 The start index of *doc* where parsing failed.
498
499 .. attribute:: lineno
500
501 The line corresponding to *pos*.
502
503 .. attribute:: colno
504
505 The column corresponding to *pos*.
506
507 .. versionadded:: 3.5
508
509
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200510Standard Compliance and Interoperability
511----------------------------------------
Antoine Pitrou331624b2012-08-24 19:37:23 +0200512
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200513The JSON format is specified by :rfc:`7159` and by
514`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
515This section details this module's level of compliance with the RFC.
516For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
517parameters other than those explicitly mentioned, are not considered.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200518
519This module does not comply with the RFC in a strict fashion, implementing some
520extensions that are valid JavaScript but not valid JSON. In particular:
521
Antoine Pitrou331624b2012-08-24 19:37:23 +0200522- Infinite and NaN number values are accepted and output;
523- Repeated names within an object are accepted, and only the value of the last
524 name-value pair is used.
525
526Since the RFC permits RFC-compliant parsers to accept input texts that are not
527RFC-compliant, this module's deserializer is technically RFC-compliant under
528default settings.
529
530Character Encodings
531^^^^^^^^^^^^^^^^^^^
532
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200533The RFC requires that JSON be represented using either UTF-8, UTF-16, or
534UTF-32, with UTF-8 being the recommended default for maximum interoperability.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200535
536As permitted, though not required, by the RFC, this module's serializer sets
537*ensure_ascii=True* by default, thus escaping the output so that the resulting
538strings only contain ASCII characters.
539
540Other than the *ensure_ascii* parameter, this module is defined strictly in
541terms of conversion between Python objects and
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200542:class:`Unicode strings <str>`, and thus does not otherwise directly address
543the issue of character encodings.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200544
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200545The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
546and this module's serializer does not add a BOM to its output.
547The RFC permits, but does not require, JSON deserializers to ignore an initial
548BOM in their input. This module's deserializer raises a :exc:`ValueError`
549when an initial BOM is present.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200550
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200551The RFC does not explicitly forbid JSON strings which contain byte sequences
552that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
553surrogates), but it does note that they may cause interoperability problems.
554By default, this module accepts and outputs (when present in the original
Serhiy Storchakad3faf432015-01-18 11:28:37 +0200555:class:`str`) code points for such sequences.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200556
557
558Infinite and NaN Number Values
559^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
560
561The RFC does not permit the representation of infinite or NaN number values.
562Despite that, by default, this module accepts and outputs ``Infinity``,
563``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
564
565 >>> # Neither of these calls raises an exception, but the results are not valid JSON
566 >>> json.dumps(float('-inf'))
567 '-Infinity'
568 >>> json.dumps(float('nan'))
569 'NaN'
570 >>> # Same when deserializing
571 >>> json.loads('-Infinity')
572 -inf
573 >>> json.loads('NaN')
574 nan
575
576In the serializer, the *allow_nan* parameter can be used to alter this
577behavior. In the deserializer, the *parse_constant* parameter can be used to
578alter this behavior.
579
580
581Repeated Names Within an Object
582^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
583
584The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200585does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrou331624b2012-08-24 19:37:23 +0200586default, this module does not raise an exception; instead, it ignores all but
587the last name-value pair for a given name::
588
589 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
590 >>> json.loads(weird_json)
591 {'x': 3}
592
593The *object_pairs_hook* parameter can be used to alter this behavior.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500594
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200595
596Top-level Non-Object, Non-Array Values
597^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
598
599The old version of JSON specified by the obsolete :rfc:`4627` required that
600the top-level value of a JSON text must be either a JSON object or array
601(Python :class:`dict` or :class:`list`), and could not be a JSON null,
602boolean, number, or string value. :rfc:`7159` removed that restriction, and
603this module does not and has never implemented that restriction in either its
604serializer or its deserializer.
605
606Regardless, for maximum interoperability, you may wish to voluntarily adhere
607to the restriction yourself.
608
609
610Implementation Limitations
611^^^^^^^^^^^^^^^^^^^^^^^^^^
612
613Some JSON deserializer implementations may set limits on:
614
615* the size of accepted JSON texts
616* the maximum level of nesting of JSON objects and arrays
617* the range and precision of JSON numbers
618* the content and maximum length of JSON strings
619
620This module does not impose any such limits beyond those of the relevant
621Python datatypes themselves or the Python interpreter itself.
622
623When serializing to JSON, beware any such limitations in applications that may
624consume your JSON. In particular, it is common for JSON numbers to be
625deserialized into IEEE 754 double precision numbers and thus subject to that
626representation's range and precision limitations. This is especially relevant
627when serializing Python :class:`int` values of extremely large magnitude, or
628when serializing instances of "exotic" numerical types such as
629:class:`decimal.Decimal`.
630
Benjamin Peterson940e2072014-03-21 23:17:29 -0500631.. highlight:: bash
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200632.. module:: json.tool
Benjamin Peterson940e2072014-03-21 23:17:29 -0500633
634.. _json-commandline:
635
636Command Line Interface
637----------------------
638
639The :mod:`json.tool` module provides a simple command line interface to validate
640and pretty-print JSON objects.
641
Georg Brandl9e7fbde2014-09-21 00:38:13 +0200642If the optional ``infile`` and ``outfile`` arguments are not
Benjamin Peterson940e2072014-03-21 23:17:29 -0500643specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively::
644
645 $ echo '{"json": "obj"}' | python -m json.tool
646 {
647 "json": "obj"
648 }
649 $ echo '{1.2:3.4}' | python -m json.tool
650 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
651
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200652.. versionchanged:: 3.5
653 The output is now in the same order as the input. Use the
654 :option:`--sort-keys` option to sort the output of dictionaries
655 alphabetically by key.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500656
657Command line options
658^^^^^^^^^^^^^^^^^^^^
659
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400660.. cmdoption:: infile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500661
662 The JSON file to be validated or pretty-printed::
663
664 $ python -m json.tool mp_films.json
665 [
666 {
667 "title": "And Now for Something Completely Different",
668 "year": 1971
669 },
670 {
671 "title": "Monty Python and the Holy Grail",
672 "year": 1975
673 }
674 ]
675
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400676 If *infile* is not specified, read from :attr:`sys.stdin`.
677
678.. cmdoption:: outfile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500679
680 Write the output of the *infile* to the given *outfile*. Otherwise, write it
681 to :attr:`sys.stdout`.
682
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200683.. cmdoption:: --sort-keys
684
685 Sort the output of dictionaries alphabetically by key.
686
687 .. versionadded:: 3.5
688
Benjamin Peterson940e2072014-03-21 23:17:29 -0500689.. cmdoption:: -h, --help
690
691 Show the help message.
Serhiy Storchaka715f01b2014-11-27 19:45:31 +0200692
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200693
694.. rubric:: Footnotes
695
696.. [#rfc-errata] As noted in `the errata for RFC 7159
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300697 <https://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200698 JSON permits literal U+2028 (LINE SEPARATOR) and
699 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
700 (as of ECMAScript Edition 5.1) does not.