blob: c77b89ed6b529bbbba13f95f4ff8c15a5e5ec568 [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.
6.. moduleauthor:: Bob Ippolito <bob@redivi.com>
7.. sectionauthor:: Bob Ippolito <bob@redivi.com>
Christian Heimes90540002008-05-08 14:29:10 +00008
Antoine Pitrou331624b2012-08-24 19:37:23 +02009`JSON (JavaScript Object Notation) <http://json.org>`_, specified by
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +020010:rfc:`7159` (which obsoletes :rfc:`4627`) and by
11`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_,
12is a lightweight data interchange format inspired by
13`JavaScript <http://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
14(although it is not a strict subset of JavaScript [#rfc-errata]_ ).
Christian Heimes90540002008-05-08 14:29:10 +000015
16:mod:`json` exposes an API familiar to users of the standard library
17:mod:`marshal` and :mod:`pickle` modules.
18
19Encoding basic Python object hierarchies::
Georg Brandl48310cd2009-01-03 21:18:54 +000020
Christian Heimes90540002008-05-08 14:29:10 +000021 >>> import json
22 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
23 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
Neal Norwitz752abd02008-05-13 04:55:24 +000024 >>> print(json.dumps("\"foo\bar"))
Christian Heimes90540002008-05-08 14:29:10 +000025 "\"foo\bar"
Benjamin Peterson2505bc62008-05-15 02:17:58 +000026 >>> print(json.dumps('\u1234'))
Christian Heimes90540002008-05-08 14:29:10 +000027 "\u1234"
Neal Norwitz752abd02008-05-13 04:55:24 +000028 >>> print(json.dumps('\\'))
Christian Heimes90540002008-05-08 14:29:10 +000029 "\\"
Neal Norwitz752abd02008-05-13 04:55:24 +000030 >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
Christian Heimes90540002008-05-08 14:29:10 +000031 {"a": 0, "b": 0, "c": 0}
Benjamin Peterson2505bc62008-05-15 02:17:58 +000032 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000033 >>> io = StringIO()
34 >>> json.dump(['streaming API'], io)
35 >>> io.getvalue()
36 '["streaming API"]'
37
38Compact encoding::
39
40 >>> import json
Éric Araujode579d42011-04-21 02:37:41 +020041 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))
Christian Heimes90540002008-05-08 14:29:10 +000042 '[1,2,3,{"4":5,"6":7}]'
43
44Pretty printing::
45
46 >>> import json
Neal Norwitz752abd02008-05-13 04:55:24 +000047 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
Christian Heimes90540002008-05-08 14:29:10 +000048 {
Georg Brandl48310cd2009-01-03 21:18:54 +000049 "4": 5,
Christian Heimes90540002008-05-08 14:29:10 +000050 "6": 7
51 }
52
53Decoding JSON::
Georg Brandl48310cd2009-01-03 21:18:54 +000054
Christian Heimes90540002008-05-08 14:29:10 +000055 >>> import json
56 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000057 ['foo', {'bar': ['baz', None, 1.0, 2]}]
Christian Heimes90540002008-05-08 14:29:10 +000058 >>> json.loads('"\\"foo\\bar"')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000059 '"foo\x08ar'
60 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000061 >>> io = StringIO('["streaming API"]')
62 >>> json.load(io)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000063 ['streaming API']
Christian Heimes90540002008-05-08 14:29:10 +000064
65Specializing JSON object decoding::
66
67 >>> import json
68 >>> def as_complex(dct):
69 ... if '__complex__' in dct:
70 ... return complex(dct['real'], dct['imag'])
71 ... return dct
Benjamin Peterson2505bc62008-05-15 02:17:58 +000072 ...
Christian Heimes90540002008-05-08 14:29:10 +000073 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
74 ... object_hook=as_complex)
75 (1+2j)
76 >>> import decimal
77 >>> json.loads('1.1', parse_float=decimal.Decimal)
78 Decimal('1.1')
79
80Extending :class:`JSONEncoder`::
Georg Brandl48310cd2009-01-03 21:18:54 +000081
Christian Heimes90540002008-05-08 14:29:10 +000082 >>> import json
83 >>> class ComplexEncoder(json.JSONEncoder):
84 ... def default(self, obj):
85 ... if isinstance(obj, complex):
86 ... return [obj.real, obj.imag]
R David Murraydd246172013-03-17 21:52:35 -040087 ... # Let the base class default method raise the TypeError
Christian Heimes90540002008-05-08 14:29:10 +000088 ... return json.JSONEncoder.default(self, obj)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000089 ...
Georg Brandl0bb73b82010-09-03 22:36:22 +000090 >>> json.dumps(2 + 1j, cls=ComplexEncoder)
Christian Heimes90540002008-05-08 14:29:10 +000091 '[2.0, 1.0]'
92 >>> ComplexEncoder().encode(2 + 1j)
93 '[2.0, 1.0]'
94 >>> list(ComplexEncoder().iterencode(2 + 1j))
Georg Brandl0bb73b82010-09-03 22:36:22 +000095 ['[2.0', ', 1.0', ']']
Georg Brandl48310cd2009-01-03 21:18:54 +000096
Christian Heimes90540002008-05-08 14:29:10 +000097
Ezio Melotti84e59aa2012-04-13 21:02:18 -060098.. highlight:: bash
Christian Heimes90540002008-05-08 14:29:10 +000099
100Using json.tool from the shell to validate and pretty-print::
Georg Brandl48310cd2009-01-03 21:18:54 +0000101
Georg Brandl946faa32014-10-28 22:54:24 +0100102 $ echo '{"json":"obj"}' | python -m json.tool
Christian Heimes90540002008-05-08 14:29:10 +0000103 {
104 "json": "obj"
105 }
Georg Brandl946faa32014-10-28 22:54:24 +0100106 $ echo '{1.2:3.4}' | python -m json.tool
Serhiy Storchakac510a042013-02-21 20:19:16 +0200107 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Christian Heimes90540002008-05-08 14:29:10 +0000108
Benjamin Peterson940e2072014-03-21 23:17:29 -0500109See :ref:`json-commandline` for detailed documentation.
110
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600111.. highlight:: python3
Christian Heimes90540002008-05-08 14:29:10 +0000112
Georg Brandl48310cd2009-01-03 21:18:54 +0000113.. note::
Christian Heimes90540002008-05-08 14:29:10 +0000114
Antoine Pitrou331624b2012-08-24 19:37:23 +0200115 JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by
116 this module's default settings (in particular, the default *separators*
117 value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
118 used as a YAML serializer.
Christian Heimes90540002008-05-08 14:29:10 +0000119
120
121Basic Usage
122-----------
123
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200124.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
125 check_circular=True, allow_nan=True, cls=None, \
126 indent=None, separators=None, default=None, \
127 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000128
129 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200130 :term:`file-like object`) using this :ref:`conversion table
131 <py-to-json-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000132
133 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000134 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
135 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000136
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000137 The :mod:`json` module always produces :class:`str` objects, not
138 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
139 input.
140
Éric Araujo6f7aa002012-01-16 10:09:20 +0100141 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
142 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
143 ``False``, these characters will be output as-is.
144
Christian Heimes90540002008-05-08 14:29:10 +0000145 If *check_circular* is ``False`` (default: ``True``), then the circular
146 reference check for container types will be skipped and a circular reference
147 will result in an :exc:`OverflowError` (or worse).
148
149 If *allow_nan* is ``False`` (default: ``True``), then it will be a
150 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
151 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
152 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
153
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000154 If *indent* is a non-negative integer or string, then JSON array elements and
155 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400156 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
157 selects the most compact representation. Using a positive integer indent
Petri Lehtinen72c6eef2012-08-27 20:27:30 +0300158 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
R David Murrayd5315482011-04-12 21:09:18 -0400159 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000160
Petri Lehtinen72b14262012-08-28 07:08:44 +0300161 .. versionchanged:: 3.2
162 Allow strings for *indent* in addition to integers.
163
Ezio Melotti10031442012-11-29 00:42:56 +0200164 If specified, *separators* should be an ``(item_separator, key_separator)``
165 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
166 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
167 you should specify ``(',', ':')`` to eliminate whitespace.
168
169 .. versionchanged:: 3.4
170 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000171
Christian Heimes90540002008-05-08 14:29:10 +0000172 *default(obj)* is a function that should return a serializable version of
173 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
174
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200175 If *sort_keys* is ``True`` (default: ``False``), then the output of
176 dictionaries will be sorted by key.
177
Georg Brandl1f01deb2009-01-03 22:47:39 +0000178 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000179 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000180 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000181
182
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200183.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
184 check_circular=True, allow_nan=True, cls=None, \
185 indent=None, separators=None, default=None, \
186 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000187
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200188 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
189 table <py-to-json-table>`. The arguments have the same meaning as in
190 :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000191
Ezio Melotti60adf952011-04-15 07:37:00 +0300192 .. note::
193
Georg Brandl340d2692011-04-16 16:54:15 +0200194 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
195 so trying to serialize multiple objects with repeated calls to
196 :func:`dump` using the same *fp* will result in an invalid JSON file.
197
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700198 .. note::
199
200 Keys in key/value pairs of JSON are always of the type :class:`str`. When
201 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy9cbcc2f2013-03-08 19:35:15 -0500202 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700203 into JSON and then back into a dictionary, the dictionary may not equal
204 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
205 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000206
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000207.. 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 +0000208
Antoine Pitrou15251a92012-08-24 19:49:08 +0200209 Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200210 containing a JSON document) to a Python object using this :ref:`conversion
211 table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000212
Christian Heimes90540002008-05-08 14:29:10 +0000213 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000214 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000215 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrou331624b2012-08-24 19:37:23 +0200216 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
217 class hinting).
Christian Heimes90540002008-05-08 14:29:10 +0000218
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000219 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000220 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000221 return value of *object_pairs_hook* will be used instead of the
222 :class:`dict`. This feature can be used to implement custom decoders that
223 rely on the order that the key and value pairs are decoded (for example,
224 :func:`collections.OrderedDict` will remember the order of insertion). If
225 *object_hook* is also defined, the *object_pairs_hook* takes priority.
226
227 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000228 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000229
Christian Heimes90540002008-05-08 14:29:10 +0000230 *parse_float*, if specified, will be called with the string of every JSON
231 float to be decoded. By default, this is equivalent to ``float(num_str)``.
232 This can be used to use another datatype or parser for JSON floats
233 (e.g. :class:`decimal.Decimal`).
234
235 *parse_int*, if specified, will be called with the string of every JSON int
236 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
237 be used to use another datatype or parser for JSON integers
238 (e.g. :class:`float`).
239
240 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200241 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
242 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000243 are encountered.
244
Hynek Schlawackf54c0602012-05-20 18:32:53 +0200245 .. versionchanged:: 3.1
Hynek Schlawack1203e832012-05-20 12:03:17 +0200246 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
247
Christian Heimes90540002008-05-08 14:29:10 +0000248 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000249 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
250 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000251
Felix Crux60fb9712013-08-12 17:39:51 -0400252 If the data being deserialized is not a valid JSON document, a
253 :exc:`ValueError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000254
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000255.. 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 +0000256
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000257 Deserialize *s* (a :class:`str` instance containing a JSON document) to a
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200258 Python object using this :ref:`conversion table <json-to-py-table>`.
Christian Heimes90540002008-05-08 14:29:10 +0000259
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000260 The other arguments have the same meaning as in :func:`load`, except
261 *encoding* which is ignored and deprecated.
Christian Heimes90540002008-05-08 14:29:10 +0000262
Felix Cruxb4357992013-08-12 17:39:51 -0400263 If the data being deserialized is not a valid JSON document, a
264 :exc:`ValueError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000265
Antoine Pitrou331624b2012-08-24 19:37:23 +0200266Encoders and Decoders
Christian Heimes90540002008-05-08 14:29:10 +0000267---------------------
268
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000269.. 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 +0000270
271 Simple JSON decoder.
272
273 Performs the following translations in decoding by default:
274
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200275 .. _json-to-py-table:
276
Christian Heimes90540002008-05-08 14:29:10 +0000277 +---------------+-------------------+
278 | JSON | Python |
279 +===============+===================+
280 | object | dict |
281 +---------------+-------------------+
282 | array | list |
283 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000284 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000285 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000286 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000287 +---------------+-------------------+
288 | number (real) | float |
289 +---------------+-------------------+
290 | true | True |
291 +---------------+-------------------+
292 | false | False |
293 +---------------+-------------------+
294 | null | None |
295 +---------------+-------------------+
296
297 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
298 corresponding ``float`` values, which is outside the JSON spec.
299
Christian Heimes90540002008-05-08 14:29:10 +0000300 *object_hook*, if specified, will be called with the result of every JSON
301 object decoded and its return value will be used in place of the given
302 :class:`dict`. This can be used to provide custom deserializations (e.g. to
303 support JSON-RPC class hinting).
304
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000305 *object_pairs_hook*, if specified will be called with the result of every
306 JSON object decoded with an ordered list of pairs. The return value of
307 *object_pairs_hook* will be used instead of the :class:`dict`. This
308 feature can be used to implement custom decoders that rely on the order
309 that the key and value pairs are decoded (for example,
310 :func:`collections.OrderedDict` will remember the order of insertion). If
311 *object_hook* is also defined, the *object_pairs_hook* takes priority.
312
313 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000314 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000315
Christian Heimes90540002008-05-08 14:29:10 +0000316 *parse_float*, if specified, will be called with the string of every JSON
317 float to be decoded. By default, this is equivalent to ``float(num_str)``.
318 This can be used to use another datatype or parser for JSON floats
319 (e.g. :class:`decimal.Decimal`).
320
321 *parse_int*, if specified, will be called with the string of every JSON int
322 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
323 be used to use another datatype or parser for JSON integers
324 (e.g. :class:`float`).
325
326 *parse_constant*, if specified, will be called with one of the following
327 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
328 ``'false'``. This can be used to raise an exception if invalid JSON numbers
329 are encountered.
330
Georg Brandld4460aa2010-10-15 17:03:02 +0000331 If *strict* is ``False`` (``True`` is the default), then control characters
332 will be allowed inside strings. Control characters in this context are
333 those with character codes in the 0-31 range, including ``'\t'`` (tab),
334 ``'\n'``, ``'\r'`` and ``'\0'``.
335
Felix Crux654f0032013-08-12 17:39:51 -0400336 If the data being deserialized is not a valid JSON document, a
337 :exc:`ValueError` will be raised.
Christian Heimes90540002008-05-08 14:29:10 +0000338
339 .. method:: decode(s)
340
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000341 Return the Python representation of *s* (a :class:`str` instance
342 containing a JSON document)
Christian Heimes90540002008-05-08 14:29:10 +0000343
344 .. method:: raw_decode(s)
345
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000346 Decode a JSON document from *s* (a :class:`str` beginning with a
347 JSON document) and return a 2-tuple of the Python representation
348 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000349
350 This can be used to decode a JSON document from a string that may have
351 extraneous data at the end.
352
353
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000354.. 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 +0000355
356 Extensible JSON encoder for Python data structures.
357
358 Supports the following objects and types by default:
359
Ezio Melotti6d2bc6e2013-03-29 03:59:29 +0200360 .. _py-to-json-table:
361
Ethan Furmana4998a72013-08-10 13:01:45 -0700362 +----------------------------------------+---------------+
363 | Python | JSON |
364 +========================================+===============+
365 | dict | object |
366 +----------------------------------------+---------------+
367 | list, tuple | array |
368 +----------------------------------------+---------------+
369 | str | string |
370 +----------------------------------------+---------------+
371 | int, float, int- & float-derived Enums | number |
372 +----------------------------------------+---------------+
373 | True | true |
374 +----------------------------------------+---------------+
375 | False | false |
376 +----------------------------------------+---------------+
377 | None | null |
378 +----------------------------------------+---------------+
379
380 .. versionchanged:: 3.4
381 Added support for int- and float-derived Enum classes.
Christian Heimes90540002008-05-08 14:29:10 +0000382
383 To extend this to recognize other objects, subclass and implement a
384 :meth:`default` method with another method that returns a serializable object
385 for ``o`` if possible, otherwise it should call the superclass implementation
386 (to raise :exc:`TypeError`).
387
388 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
Georg Brandl639ce962009-04-11 18:18:16 +0000389 attempt encoding of keys that are not str, int, float or None. If
Christian Heimes90540002008-05-08 14:29:10 +0000390 *skipkeys* is ``True``, such items are simply skipped.
391
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000392 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
393 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
394 ``False``, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000395
396 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
397 encoded objects will be checked for circular references during encoding to
398 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
399 Otherwise, no such check takes place.
400
401 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
402 ``-Infinity`` will be encoded as such. This behavior is not JSON
403 specification compliant, but is consistent with most JavaScript based
404 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
405 such floats.
406
Georg Brandl6a74da32010-08-22 20:23:38 +0000407 If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000408 will be sorted by key; this is useful for regression tests to ensure that
409 JSON serializations can be compared on a day-to-day basis.
410
Petri Lehtinen72b14262012-08-28 07:08:44 +0300411 If *indent* is a non-negative integer or string, then JSON array elements and
412 object members will be pretty-printed with that indent level. An indent level
413 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
414 selects the most compact representation. Using a positive integer indent
415 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
416 that string is used to indent each level.
417
418 .. versionchanged:: 3.2
419 Allow strings for *indent* in addition to integers.
Christian Heimes90540002008-05-08 14:29:10 +0000420
421 If specified, *separators* should be an ``(item_separator, key_separator)``
Ezio Melotti10031442012-11-29 00:42:56 +0200422 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
423 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
424 you should specify ``(',', ':')`` to eliminate whitespace.
425
426 .. versionchanged:: 3.4
427 Use ``(',', ': ')`` as default if *indent* is not ``None``.
Christian Heimes90540002008-05-08 14:29:10 +0000428
429 If specified, *default* is a function that gets called for objects that can't
430 otherwise be serialized. It should return a JSON encodable version of the
431 object or raise a :exc:`TypeError`.
432
Christian Heimes90540002008-05-08 14:29:10 +0000433
434 .. method:: default(o)
435
436 Implement this method in a subclass such that it returns a serializable
437 object for *o*, or calls the base implementation (to raise a
438 :exc:`TypeError`).
439
440 For example, to support arbitrary iterators, you could implement default
441 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000442
Christian Heimes90540002008-05-08 14:29:10 +0000443 def default(self, o):
444 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000445 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000446 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000447 pass
Christian Heimes90540002008-05-08 14:29:10 +0000448 else:
449 return list(iterable)
R David Murraydd246172013-03-17 21:52:35 -0400450 # Let the base class default method raise the TypeError
Georg Brandl0bb73b82010-09-03 22:36:22 +0000451 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000452
453
454 .. method:: encode(o)
455
456 Return a JSON string representation of a Python data structure, *o*. For
457 example::
458
Georg Brandl0bb73b82010-09-03 22:36:22 +0000459 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000460 '{"foo": ["bar", "baz"]}'
461
462
463 .. method:: iterencode(o)
464
465 Encode the given object, *o*, and yield each string representation as
466 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000467
Georg Brandl0bb73b82010-09-03 22:36:22 +0000468 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000469 mysocket.write(chunk)
Antoine Pitrou331624b2012-08-24 19:37:23 +0200470
471
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200472Standard Compliance and Interoperability
473----------------------------------------
Antoine Pitrou331624b2012-08-24 19:37:23 +0200474
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200475The JSON format is specified by :rfc:`7159` and by
476`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
477This section details this module's level of compliance with the RFC.
478For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
479parameters other than those explicitly mentioned, are not considered.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200480
481This module does not comply with the RFC in a strict fashion, implementing some
482extensions that are valid JavaScript but not valid JSON. In particular:
483
Antoine Pitrou331624b2012-08-24 19:37:23 +0200484- Infinite and NaN number values are accepted and output;
485- Repeated names within an object are accepted, and only the value of the last
486 name-value pair is used.
487
488Since the RFC permits RFC-compliant parsers to accept input texts that are not
489RFC-compliant, this module's deserializer is technically RFC-compliant under
490default settings.
491
492Character Encodings
493^^^^^^^^^^^^^^^^^^^
494
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200495The RFC requires that JSON be represented using either UTF-8, UTF-16, or
496UTF-32, with UTF-8 being the recommended default for maximum interoperability.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200497
498As permitted, though not required, by the RFC, this module's serializer sets
499*ensure_ascii=True* by default, thus escaping the output so that the resulting
500strings only contain ASCII characters.
501
502Other than the *ensure_ascii* parameter, this module is defined strictly in
503terms of conversion between Python objects and
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200504:class:`Unicode strings <str>`, and thus does not otherwise directly address
505the issue of character encodings.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200506
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200507The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
508and this module's serializer does not add a BOM to its output.
509The RFC permits, but does not require, JSON deserializers to ignore an initial
510BOM in their input. This module's deserializer raises a :exc:`ValueError`
511when an initial BOM is present.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200512
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200513The RFC does not explicitly forbid JSON strings which contain byte sequences
514that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
515surrogates), but it does note that they may cause interoperability problems.
516By default, this module accepts and outputs (when present in the original
517:class:`str`) codepoints for such sequences.
Antoine Pitrou331624b2012-08-24 19:37:23 +0200518
519
520Infinite and NaN Number Values
521^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
522
523The RFC does not permit the representation of infinite or NaN number values.
524Despite that, by default, this module accepts and outputs ``Infinity``,
525``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
526
527 >>> # Neither of these calls raises an exception, but the results are not valid JSON
528 >>> json.dumps(float('-inf'))
529 '-Infinity'
530 >>> json.dumps(float('nan'))
531 'NaN'
532 >>> # Same when deserializing
533 >>> json.loads('-Infinity')
534 -inf
535 >>> json.loads('NaN')
536 nan
537
538In the serializer, the *allow_nan* parameter can be used to alter this
539behavior. In the deserializer, the *parse_constant* parameter can be used to
540alter this behavior.
541
542
543Repeated Names Within an Object
544^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
545
546The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200547does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrou331624b2012-08-24 19:37:23 +0200548default, this module does not raise an exception; instead, it ignores all but
549the last name-value pair for a given name::
550
551 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
552 >>> json.loads(weird_json)
553 {'x': 3}
554
555The *object_pairs_hook* parameter can be used to alter this behavior.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500556
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200557
558Top-level Non-Object, Non-Array Values
559^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
560
561The old version of JSON specified by the obsolete :rfc:`4627` required that
562the top-level value of a JSON text must be either a JSON object or array
563(Python :class:`dict` or :class:`list`), and could not be a JSON null,
564boolean, number, or string value. :rfc:`7159` removed that restriction, and
565this module does not and has never implemented that restriction in either its
566serializer or its deserializer.
567
568Regardless, for maximum interoperability, you may wish to voluntarily adhere
569to the restriction yourself.
570
571
572Implementation Limitations
573^^^^^^^^^^^^^^^^^^^^^^^^^^
574
575Some JSON deserializer implementations may set limits on:
576
577* the size of accepted JSON texts
578* the maximum level of nesting of JSON objects and arrays
579* the range and precision of JSON numbers
580* the content and maximum length of JSON strings
581
582This module does not impose any such limits beyond those of the relevant
583Python datatypes themselves or the Python interpreter itself.
584
585When serializing to JSON, beware any such limitations in applications that may
586consume your JSON. In particular, it is common for JSON numbers to be
587deserialized into IEEE 754 double precision numbers and thus subject to that
588representation's range and precision limitations. This is especially relevant
589when serializing Python :class:`int` values of extremely large magnitude, or
590when serializing instances of "exotic" numerical types such as
591:class:`decimal.Decimal`.
592
Benjamin Peterson940e2072014-03-21 23:17:29 -0500593.. highlight:: bash
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200594.. module:: json.tool
Benjamin Peterson940e2072014-03-21 23:17:29 -0500595
596.. _json-commandline:
597
598Command Line Interface
599----------------------
600
601The :mod:`json.tool` module provides a simple command line interface to validate
602and pretty-print JSON objects.
603
Georg Brandl9e7fbde2014-09-21 00:38:13 +0200604If the optional ``infile`` and ``outfile`` arguments are not
Benjamin Peterson940e2072014-03-21 23:17:29 -0500605specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively::
606
607 $ echo '{"json": "obj"}' | python -m json.tool
608 {
609 "json": "obj"
610 }
611 $ echo '{1.2:3.4}' | python -m json.tool
612 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
613
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200614.. versionchanged:: 3.5
615 The output is now in the same order as the input. Use the
616 :option:`--sort-keys` option to sort the output of dictionaries
617 alphabetically by key.
Benjamin Peterson940e2072014-03-21 23:17:29 -0500618
619Command line options
620^^^^^^^^^^^^^^^^^^^^
621
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400622.. cmdoption:: infile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500623
624 The JSON file to be validated or pretty-printed::
625
626 $ python -m json.tool mp_films.json
627 [
628 {
629 "title": "And Now for Something Completely Different",
630 "year": 1971
631 },
632 {
633 "title": "Monty Python and the Holy Grail",
634 "year": 1975
635 }
636 ]
637
Benjamin Petersonfc8e9882014-04-13 19:52:14 -0400638 If *infile* is not specified, read from :attr:`sys.stdin`.
639
640.. cmdoption:: outfile
Benjamin Peterson940e2072014-03-21 23:17:29 -0500641
642 Write the output of the *infile* to the given *outfile*. Otherwise, write it
643 to :attr:`sys.stdout`.
644
Berker Peksag39e4c4d2014-11-10 09:56:54 +0200645.. cmdoption:: --sort-keys
646
647 Sort the output of dictionaries alphabetically by key.
648
649 .. versionadded:: 3.5
650
Benjamin Peterson940e2072014-03-21 23:17:29 -0500651.. cmdoption:: -h, --help
652
653 Show the help message.
Serhiy Storchaka715f01b2014-11-27 19:45:31 +0200654
Serhiy Storchaka7a6915e2014-11-27 19:41:47 +0200655
656.. rubric:: Footnotes
657
658.. [#rfc-errata] As noted in `the errata for RFC 7159
659 <http://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
660 JSON permits literal U+2028 (LINE SEPARATOR) and
661 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
662 (as of ECMAScript Edition 5.1) does not.