Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | :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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 8 | |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 9 | `JSON (JavaScript Object Notation) <http://json.org>`_, specified by |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 10 | :rfc:`7159` (which obsoletes :rfc:`4627`) and by |
| 11 | `ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_, |
| 12 | is a lightweight data interchange format inspired by |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 13 | `JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 14 | (although it is not a strict subset of JavaScript [#rfc-errata]_ ). |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 15 | |
| 16 | :mod:`json` exposes an API familiar to users of the standard library |
| 17 | :mod:`marshal` and :mod:`pickle` modules. |
| 18 | |
| 19 | Encoding basic Python object hierarchies:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 20 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 21 | >>> import json |
| 22 | >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) |
| 23 | '["foo", {"bar": ["baz", null, 1.0, 2]}]' |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 24 | >>> print(json.dumps("\"foo\bar")) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 25 | "\"foo\bar" |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 26 | >>> print(json.dumps('\u1234')) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 27 | "\u1234" |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 28 | >>> print(json.dumps('\\')) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 29 | "\\" |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 30 | >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 31 | {"a": 0, "b": 0, "c": 0} |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 32 | >>> from io import StringIO |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 33 | >>> io = StringIO() |
| 34 | >>> json.dump(['streaming API'], io) |
| 35 | >>> io.getvalue() |
| 36 | '["streaming API"]' |
| 37 | |
| 38 | Compact encoding:: |
| 39 | |
| 40 | >>> import json |
Éric Araujo | de579d4 | 2011-04-21 02:37:41 +0200 | [diff] [blame] | 41 | >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':')) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 42 | '[1,2,3,{"4":5,"6":7}]' |
| 43 | |
| 44 | Pretty printing:: |
| 45 | |
| 46 | >>> import json |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 47 | >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 48 | { |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 49 | "4": 5, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 50 | "6": 7 |
| 51 | } |
| 52 | |
| 53 | Decoding JSON:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 54 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 55 | >>> import json |
| 56 | >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 57 | ['foo', {'bar': ['baz', None, 1.0, 2]}] |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 58 | >>> json.loads('"\\"foo\\bar"') |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 59 | '"foo\x08ar' |
| 60 | >>> from io import StringIO |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 61 | >>> io = StringIO('["streaming API"]') |
| 62 | >>> json.load(io) |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 63 | ['streaming API'] |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 64 | |
| 65 | Specializing 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 Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 72 | ... |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 73 | >>> 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 | |
| 80 | Extending :class:`JSONEncoder`:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 81 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 82 | >>> 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 Murray | dd24617 | 2013-03-17 21:52:35 -0400 | [diff] [blame] | 87 | ... # Let the base class default method raise the TypeError |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 88 | ... return json.JSONEncoder.default(self, obj) |
Benjamin Peterson | 2505bc6 | 2008-05-15 02:17:58 +0000 | [diff] [blame] | 89 | ... |
Georg Brandl | 0bb73b8 | 2010-09-03 22:36:22 +0000 | [diff] [blame] | 90 | >>> json.dumps(2 + 1j, cls=ComplexEncoder) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 91 | '[2.0, 1.0]' |
| 92 | >>> ComplexEncoder().encode(2 + 1j) |
| 93 | '[2.0, 1.0]' |
| 94 | >>> list(ComplexEncoder().iterencode(2 + 1j)) |
Georg Brandl | 0bb73b8 | 2010-09-03 22:36:22 +0000 | [diff] [blame] | 95 | ['[2.0', ', 1.0', ']'] |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 96 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 97 | |
Ezio Melotti | 84e59aa | 2012-04-13 21:02:18 -0600 | [diff] [blame] | 98 | .. highlight:: bash |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 99 | |
| 100 | Using json.tool from the shell to validate and pretty-print:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 101 | |
Georg Brandl | 946faa3 | 2014-10-28 22:54:24 +0100 | [diff] [blame] | 102 | $ echo '{"json":"obj"}' | python -m json.tool |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 103 | { |
| 104 | "json": "obj" |
| 105 | } |
Georg Brandl | 946faa3 | 2014-10-28 22:54:24 +0100 | [diff] [blame] | 106 | $ echo '{1.2:3.4}' | python -m json.tool |
Serhiy Storchaka | c510a04 | 2013-02-21 20:19:16 +0200 | [diff] [blame] | 107 | Expecting property name enclosed in double quotes: line 1 column 2 (char 1) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 109 | See :ref:`json-commandline` for detailed documentation. |
| 110 | |
Ezio Melotti | 84e59aa | 2012-04-13 21:02:18 -0600 | [diff] [blame] | 111 | .. highlight:: python3 |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 113 | .. note:: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 115 | 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | Basic Usage |
| 122 | ----------- |
| 123 | |
Andrew Svetlov | 2ec53be | 2012-10-28 14:10:30 +0200 | [diff] [blame] | 124 | .. 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 128 | |
| 129 | Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 130 | :term:`file-like object`) using this :ref:`conversion table |
| 131 | <py-to-json-table>`. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 132 | |
| 133 | If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not |
Antoine Pitrou | 00d650b | 2011-01-21 21:37:32 +0000 | [diff] [blame] | 134 | of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`, |
| 135 | ``None``) will be skipped instead of raising a :exc:`TypeError`. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 137 | 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 Araujo | 6f7aa00 | 2012-01-16 10:09:20 +0100 | [diff] [blame] | 141 | 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 145 | 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 Hettinger | b643ef8 | 2010-10-31 08:00:16 +0000 | [diff] [blame] | 154 | 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 Murray | d531548 | 2011-04-12 21:09:18 -0400 | [diff] [blame] | 156 | of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) |
| 157 | selects the most compact representation. Using a positive integer indent |
Petri Lehtinen | 72c6eef | 2012-08-27 20:27:30 +0300 | [diff] [blame] | 158 | indents that many spaces per level. If *indent* is a string (such as ``"\t"``), |
R David Murray | d531548 | 2011-04-12 21:09:18 -0400 | [diff] [blame] | 159 | that string is used to indent each level. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 160 | |
Petri Lehtinen | 72b1426 | 2012-08-28 07:08:44 +0300 | [diff] [blame] | 161 | .. versionchanged:: 3.2 |
| 162 | Allow strings for *indent* in addition to integers. |
| 163 | |
Ezio Melotti | 1003144 | 2012-11-29 00:42:56 +0200 | [diff] [blame] | 164 | 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 171 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 172 | *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 Svetlov | 2ec53be | 2012-10-28 14:10:30 +0200 | [diff] [blame] | 175 | If *sort_keys* is ``True`` (default: ``False``), then the output of |
| 176 | dictionaries will be sorted by key. |
| 177 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 178 | To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 179 | :meth:`default` method to serialize additional types), specify it with the |
Georg Brandl | d4460aa | 2010-10-15 17:03:02 +0000 | [diff] [blame] | 180 | *cls* kwarg; otherwise :class:`JSONEncoder` is used. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 181 | |
| 182 | |
Andrew Svetlov | 2ec53be | 2012-10-28 14:10:30 +0200 | [diff] [blame] | 183 | .. 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 187 | |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 188 | 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 191 | |
Ezio Melotti | 60adf95 | 2011-04-15 07:37:00 +0300 | [diff] [blame] | 192 | .. note:: |
| 193 | |
Georg Brandl | 340d269 | 2011-04-16 16:54:15 +0200 | [diff] [blame] | 194 | 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 Kumaran | f2123d2 | 2012-03-17 00:40:34 -0700 | [diff] [blame] | 198 | .. 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 Reedy | 9cbcc2f | 2013-03-08 19:35:15 -0500 | [diff] [blame] | 202 | coerced to strings. As a result of this, if a dictionary is converted |
Senthil Kumaran | f2123d2 | 2012-03-17 00:40:34 -0700 | [diff] [blame] | 203 | 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 206 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 207 | .. function:: load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | 15251a9 | 2012-08-24 19:49:08 +0200 | [diff] [blame] | 209 | Deserialize *fp* (a ``.read()``-supporting :term:`file-like object` |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 210 | containing a JSON document) to a Python object using this :ref:`conversion |
| 211 | table <json-to-py-table>`. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 212 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 213 | *object_hook* is an optional function that will be called with the result of |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 214 | any object literal decoded (a :class:`dict`). The return value of |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 215 | *object_hook* will be used instead of the :class:`dict`. This feature can be used |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 216 | to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_ |
| 217 | class hinting). |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 218 | |
Raymond Hettinger | 9b8d069 | 2009-04-21 03:27:12 +0000 | [diff] [blame] | 219 | *object_pairs_hook* is an optional function that will be called with the |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 220 | result of any object literal decoded with an ordered list of pairs. The |
Raymond Hettinger | 9b8d069 | 2009-04-21 03:27:12 +0000 | [diff] [blame] | 221 | 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 Yamamoto | ae9eb5c | 2009-04-26 03:34:06 +0000 | [diff] [blame] | 228 | Added support for *object_pairs_hook*. |
Raymond Hettinger | 9b8d069 | 2009-04-21 03:27:12 +0000 | [diff] [blame] | 229 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 230 | *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 Schlawack | 9729fd4 | 2012-05-16 19:01:04 +0200 | [diff] [blame] | 241 | strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. |
| 242 | This can be used to raise an exception if invalid JSON numbers |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 243 | are encountered. |
| 244 | |
Hynek Schlawack | f54c060 | 2012-05-20 18:32:53 +0200 | [diff] [blame] | 245 | .. versionchanged:: 3.1 |
Hynek Schlawack | 1203e83 | 2012-05-20 12:03:17 +0200 | [diff] [blame] | 246 | *parse_constant* doesn't get called on 'null', 'true', 'false' anymore. |
| 247 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 248 | To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls`` |
Georg Brandl | d4460aa | 2010-10-15 17:03:02 +0000 | [diff] [blame] | 249 | kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments |
| 250 | will be passed to the constructor of the class. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 251 | |
Felix Crux | 60fb971 | 2013-08-12 17:39:51 -0400 | [diff] [blame] | 252 | If the data being deserialized is not a valid JSON document, a |
Serhiy Storchaka | 47efb4a | 2015-01-26 13:16:30 +0200 | [diff] [blame] | 253 | :exc:`JSONDecodeError` will be raised. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 254 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 255 | .. 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | 00d650b | 2011-01-21 21:37:32 +0000 | [diff] [blame] | 257 | Deserialize *s* (a :class:`str` instance containing a JSON document) to a |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 258 | Python object using this :ref:`conversion table <json-to-py-table>`. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | 00d650b | 2011-01-21 21:37:32 +0000 | [diff] [blame] | 260 | The other arguments have the same meaning as in :func:`load`, except |
| 261 | *encoding* which is ignored and deprecated. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 262 | |
Felix Crux | b435799 | 2013-08-12 17:39:51 -0400 | [diff] [blame] | 263 | If the data being deserialized is not a valid JSON document, a |
Serhiy Storchaka | 47efb4a | 2015-01-26 13:16:30 +0200 | [diff] [blame] | 264 | :exc:`JSONDecodeError` will be raised. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 266 | Encoders and Decoders |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 267 | --------------------- |
| 268 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 269 | .. class:: JSONDecoder(object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 270 | |
| 271 | Simple JSON decoder. |
| 272 | |
| 273 | Performs the following translations in decoding by default: |
| 274 | |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 275 | .. _json-to-py-table: |
| 276 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 277 | +---------------+-------------------+ |
| 278 | | JSON | Python | |
| 279 | +===============+===================+ |
| 280 | | object | dict | |
| 281 | +---------------+-------------------+ |
| 282 | | array | list | |
| 283 | +---------------+-------------------+ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 284 | | string | str | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 285 | +---------------+-------------------+ |
Georg Brandl | 639ce96 | 2009-04-11 18:18:16 +0000 | [diff] [blame] | 286 | | number (int) | int | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 287 | +---------------+-------------------+ |
| 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 Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 300 | *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 Hettinger | 9b8d069 | 2009-04-21 03:27:12 +0000 | [diff] [blame] | 305 | *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 Yamamoto | ae9eb5c | 2009-04-26 03:34:06 +0000 | [diff] [blame] | 314 | Added support for *object_pairs_hook*. |
Raymond Hettinger | 9b8d069 | 2009-04-21 03:27:12 +0000 | [diff] [blame] | 315 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 316 | *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 Brandl | d4460aa | 2010-10-15 17:03:02 +0000 | [diff] [blame] | 331 | 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 Crux | 654f003 | 2013-08-12 17:39:51 -0400 | [diff] [blame] | 336 | If the data being deserialized is not a valid JSON document, a |
Serhiy Storchaka | 47efb4a | 2015-01-26 13:16:30 +0200 | [diff] [blame] | 337 | :exc:`JSONDecodeError` will be raised. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 338 | |
| 339 | .. method:: decode(s) |
| 340 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 341 | Return the Python representation of *s* (a :class:`str` instance |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 342 | containing a JSON document). |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 343 | |
Serhiy Storchaka | 47efb4a | 2015-01-26 13:16:30 +0200 | [diff] [blame] | 344 | :exc:`JSONDecodeError` will be raised if the given JSON document is not |
| 345 | valid. |
| 346 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 347 | .. method:: raw_decode(s) |
| 348 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 349 | Decode a JSON document from *s* (a :class:`str` beginning with a |
| 350 | JSON document) and return a 2-tuple of the Python representation |
| 351 | and the index in *s* where the document ended. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 352 | |
| 353 | This can be used to decode a JSON document from a string that may have |
| 354 | extraneous data at the end. |
| 355 | |
| 356 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 357 | .. class:: JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 358 | |
| 359 | Extensible JSON encoder for Python data structures. |
| 360 | |
| 361 | Supports the following objects and types by default: |
| 362 | |
Ezio Melotti | 6d2bc6e | 2013-03-29 03:59:29 +0200 | [diff] [blame] | 363 | .. _py-to-json-table: |
| 364 | |
Ethan Furman | a4998a7 | 2013-08-10 13:01:45 -0700 | [diff] [blame] | 365 | +----------------------------------------+---------------+ |
| 366 | | Python | JSON | |
| 367 | +========================================+===============+ |
| 368 | | dict | object | |
| 369 | +----------------------------------------+---------------+ |
| 370 | | list, tuple | array | |
| 371 | +----------------------------------------+---------------+ |
| 372 | | str | string | |
| 373 | +----------------------------------------+---------------+ |
| 374 | | int, float, int- & float-derived Enums | number | |
| 375 | +----------------------------------------+---------------+ |
| 376 | | True | true | |
| 377 | +----------------------------------------+---------------+ |
| 378 | | False | false | |
| 379 | +----------------------------------------+---------------+ |
| 380 | | None | null | |
| 381 | +----------------------------------------+---------------+ |
| 382 | |
| 383 | .. versionchanged:: 3.4 |
| 384 | Added support for int- and float-derived Enum classes. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 385 | |
| 386 | To extend this to recognize other objects, subclass and implement a |
| 387 | :meth:`default` method with another method that returns a serializable object |
| 388 | for ``o`` if possible, otherwise it should call the superclass implementation |
| 389 | (to raise :exc:`TypeError`). |
| 390 | |
| 391 | If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to |
Georg Brandl | 639ce96 | 2009-04-11 18:18:16 +0000 | [diff] [blame] | 392 | attempt encoding of keys that are not str, int, float or None. If |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 393 | *skipkeys* is ``True``, such items are simply skipped. |
| 394 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 395 | If *ensure_ascii* is ``True`` (the default), the output is guaranteed to |
| 396 | have all incoming non-ASCII characters escaped. If *ensure_ascii* is |
| 397 | ``False``, these characters will be output as-is. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 398 | |
| 399 | If *check_circular* is ``True`` (the default), then lists, dicts, and custom |
| 400 | encoded objects will be checked for circular references during encoding to |
| 401 | prevent an infinite recursion (which would cause an :exc:`OverflowError`). |
| 402 | Otherwise, no such check takes place. |
| 403 | |
| 404 | If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and |
| 405 | ``-Infinity`` will be encoded as such. This behavior is not JSON |
| 406 | specification compliant, but is consistent with most JavaScript based |
| 407 | encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode |
| 408 | such floats. |
| 409 | |
Georg Brandl | 6a74da3 | 2010-08-22 20:23:38 +0000 | [diff] [blame] | 410 | If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 411 | will be sorted by key; this is useful for regression tests to ensure that |
| 412 | JSON serializations can be compared on a day-to-day basis. |
| 413 | |
Petri Lehtinen | 72b1426 | 2012-08-28 07:08:44 +0300 | [diff] [blame] | 414 | If *indent* is a non-negative integer or string, then JSON array elements and |
| 415 | object members will be pretty-printed with that indent level. An indent level |
| 416 | of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) |
| 417 | selects the most compact representation. Using a positive integer indent |
| 418 | indents that many spaces per level. If *indent* is a string (such as ``"\t"``), |
| 419 | that string is used to indent each level. |
| 420 | |
| 421 | .. versionchanged:: 3.2 |
| 422 | Allow strings for *indent* in addition to integers. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 423 | |
| 424 | If specified, *separators* should be an ``(item_separator, key_separator)`` |
Ezio Melotti | 1003144 | 2012-11-29 00:42:56 +0200 | [diff] [blame] | 425 | tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and |
| 426 | ``(',', ': ')`` otherwise. To get the most compact JSON representation, |
| 427 | you should specify ``(',', ':')`` to eliminate whitespace. |
| 428 | |
| 429 | .. versionchanged:: 3.4 |
| 430 | Use ``(',', ': ')`` as default if *indent* is not ``None``. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 431 | |
| 432 | If specified, *default* is a function that gets called for objects that can't |
| 433 | otherwise be serialized. It should return a JSON encodable version of the |
| 434 | object or raise a :exc:`TypeError`. |
| 435 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 436 | |
| 437 | .. method:: default(o) |
| 438 | |
| 439 | Implement this method in a subclass such that it returns a serializable |
| 440 | object for *o*, or calls the base implementation (to raise a |
| 441 | :exc:`TypeError`). |
| 442 | |
| 443 | For example, to support arbitrary iterators, you could implement default |
| 444 | like this:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 445 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 446 | def default(self, o): |
| 447 | try: |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 448 | iterable = iter(o) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 449 | except TypeError: |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 450 | pass |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 451 | else: |
| 452 | return list(iterable) |
R David Murray | dd24617 | 2013-03-17 21:52:35 -0400 | [diff] [blame] | 453 | # Let the base class default method raise the TypeError |
Georg Brandl | 0bb73b8 | 2010-09-03 22:36:22 +0000 | [diff] [blame] | 454 | return json.JSONEncoder.default(self, o) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | .. method:: encode(o) |
| 458 | |
| 459 | Return a JSON string representation of a Python data structure, *o*. For |
| 460 | example:: |
| 461 | |
Georg Brandl | 0bb73b8 | 2010-09-03 22:36:22 +0000 | [diff] [blame] | 462 | >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]}) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 463 | '{"foo": ["bar", "baz"]}' |
| 464 | |
| 465 | |
| 466 | .. method:: iterencode(o) |
| 467 | |
| 468 | Encode the given object, *o*, and yield each string representation as |
| 469 | available. For example:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 470 | |
Georg Brandl | 0bb73b8 | 2010-09-03 22:36:22 +0000 | [diff] [blame] | 471 | for chunk in json.JSONEncoder().iterencode(bigobject): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 472 | mysocket.write(chunk) |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 473 | |
| 474 | |
Serhiy Storchaka | 47efb4a | 2015-01-26 13:16:30 +0200 | [diff] [blame] | 475 | Exceptions |
| 476 | ---------- |
| 477 | |
| 478 | .. exception:: JSONDecodeError(msg, doc, pos, end=None) |
| 479 | |
| 480 | Subclass of :exc:`ValueError` with the following additional attributes: |
| 481 | |
| 482 | .. attribute:: msg |
| 483 | |
| 484 | The unformatted error message. |
| 485 | |
| 486 | .. attribute:: doc |
| 487 | |
| 488 | The JSON document being parsed. |
| 489 | |
| 490 | .. attribute:: pos |
| 491 | |
| 492 | The start index of *doc* where parsing failed. |
| 493 | |
| 494 | .. attribute:: lineno |
| 495 | |
| 496 | The line corresponding to *pos*. |
| 497 | |
| 498 | .. attribute:: colno |
| 499 | |
| 500 | The column corresponding to *pos*. |
| 501 | |
| 502 | .. versionadded:: 3.5 |
| 503 | |
| 504 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 505 | Standard Compliance and Interoperability |
| 506 | ---------------------------------------- |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 507 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 508 | The JSON format is specified by :rfc:`7159` and by |
| 509 | `ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_. |
| 510 | This section details this module's level of compliance with the RFC. |
| 511 | For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and |
| 512 | parameters other than those explicitly mentioned, are not considered. |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 513 | |
| 514 | This module does not comply with the RFC in a strict fashion, implementing some |
| 515 | extensions that are valid JavaScript but not valid JSON. In particular: |
| 516 | |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 517 | - Infinite and NaN number values are accepted and output; |
| 518 | - Repeated names within an object are accepted, and only the value of the last |
| 519 | name-value pair is used. |
| 520 | |
| 521 | Since the RFC permits RFC-compliant parsers to accept input texts that are not |
| 522 | RFC-compliant, this module's deserializer is technically RFC-compliant under |
| 523 | default settings. |
| 524 | |
| 525 | Character Encodings |
| 526 | ^^^^^^^^^^^^^^^^^^^ |
| 527 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 528 | The RFC requires that JSON be represented using either UTF-8, UTF-16, or |
| 529 | UTF-32, with UTF-8 being the recommended default for maximum interoperability. |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 530 | |
| 531 | As permitted, though not required, by the RFC, this module's serializer sets |
| 532 | *ensure_ascii=True* by default, thus escaping the output so that the resulting |
| 533 | strings only contain ASCII characters. |
| 534 | |
| 535 | Other than the *ensure_ascii* parameter, this module is defined strictly in |
| 536 | terms of conversion between Python objects and |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 537 | :class:`Unicode strings <str>`, and thus does not otherwise directly address |
| 538 | the issue of character encodings. |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 539 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 540 | The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text, |
| 541 | and this module's serializer does not add a BOM to its output. |
| 542 | The RFC permits, but does not require, JSON deserializers to ignore an initial |
| 543 | BOM in their input. This module's deserializer raises a :exc:`ValueError` |
| 544 | when an initial BOM is present. |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 545 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 546 | The RFC does not explicitly forbid JSON strings which contain byte sequences |
| 547 | that don't correspond to valid Unicode characters (e.g. unpaired UTF-16 |
| 548 | surrogates), but it does note that they may cause interoperability problems. |
| 549 | By default, this module accepts and outputs (when present in the original |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 550 | :class:`str`) code points for such sequences. |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 551 | |
| 552 | |
| 553 | Infinite and NaN Number Values |
| 554 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 555 | |
| 556 | The RFC does not permit the representation of infinite or NaN number values. |
| 557 | Despite that, by default, this module accepts and outputs ``Infinity``, |
| 558 | ``-Infinity``, and ``NaN`` as if they were valid JSON number literal values:: |
| 559 | |
| 560 | >>> # Neither of these calls raises an exception, but the results are not valid JSON |
| 561 | >>> json.dumps(float('-inf')) |
| 562 | '-Infinity' |
| 563 | >>> json.dumps(float('nan')) |
| 564 | 'NaN' |
| 565 | >>> # Same when deserializing |
| 566 | >>> json.loads('-Infinity') |
| 567 | -inf |
| 568 | >>> json.loads('NaN') |
| 569 | nan |
| 570 | |
| 571 | In the serializer, the *allow_nan* parameter can be used to alter this |
| 572 | behavior. In the deserializer, the *parse_constant* parameter can be used to |
| 573 | alter this behavior. |
| 574 | |
| 575 | |
| 576 | Repeated Names Within an Object |
| 577 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 578 | |
| 579 | The RFC specifies that the names within a JSON object should be unique, but |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 580 | does not mandate how repeated names in JSON objects should be handled. By |
Antoine Pitrou | 331624b | 2012-08-24 19:37:23 +0200 | [diff] [blame] | 581 | default, this module does not raise an exception; instead, it ignores all but |
| 582 | the last name-value pair for a given name:: |
| 583 | |
| 584 | >>> weird_json = '{"x": 1, "x": 2, "x": 3}' |
| 585 | >>> json.loads(weird_json) |
| 586 | {'x': 3} |
| 587 | |
| 588 | The *object_pairs_hook* parameter can be used to alter this behavior. |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 589 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 590 | |
| 591 | Top-level Non-Object, Non-Array Values |
| 592 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 593 | |
| 594 | The old version of JSON specified by the obsolete :rfc:`4627` required that |
| 595 | the top-level value of a JSON text must be either a JSON object or array |
| 596 | (Python :class:`dict` or :class:`list`), and could not be a JSON null, |
| 597 | boolean, number, or string value. :rfc:`7159` removed that restriction, and |
| 598 | this module does not and has never implemented that restriction in either its |
| 599 | serializer or its deserializer. |
| 600 | |
| 601 | Regardless, for maximum interoperability, you may wish to voluntarily adhere |
| 602 | to the restriction yourself. |
| 603 | |
| 604 | |
| 605 | Implementation Limitations |
| 606 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 607 | |
| 608 | Some JSON deserializer implementations may set limits on: |
| 609 | |
| 610 | * the size of accepted JSON texts |
| 611 | * the maximum level of nesting of JSON objects and arrays |
| 612 | * the range and precision of JSON numbers |
| 613 | * the content and maximum length of JSON strings |
| 614 | |
| 615 | This module does not impose any such limits beyond those of the relevant |
| 616 | Python datatypes themselves or the Python interpreter itself. |
| 617 | |
| 618 | When serializing to JSON, beware any such limitations in applications that may |
| 619 | consume your JSON. In particular, it is common for JSON numbers to be |
| 620 | deserialized into IEEE 754 double precision numbers and thus subject to that |
| 621 | representation's range and precision limitations. This is especially relevant |
| 622 | when serializing Python :class:`int` values of extremely large magnitude, or |
| 623 | when serializing instances of "exotic" numerical types such as |
| 624 | :class:`decimal.Decimal`. |
| 625 | |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 626 | .. highlight:: bash |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 627 | .. module:: json.tool |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 628 | |
| 629 | .. _json-commandline: |
| 630 | |
| 631 | Command Line Interface |
| 632 | ---------------------- |
| 633 | |
| 634 | The :mod:`json.tool` module provides a simple command line interface to validate |
| 635 | and pretty-print JSON objects. |
| 636 | |
Georg Brandl | 9e7fbde | 2014-09-21 00:38:13 +0200 | [diff] [blame] | 637 | If the optional ``infile`` and ``outfile`` arguments are not |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 638 | specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively:: |
| 639 | |
| 640 | $ echo '{"json": "obj"}' | python -m json.tool |
| 641 | { |
| 642 | "json": "obj" |
| 643 | } |
| 644 | $ echo '{1.2:3.4}' | python -m json.tool |
| 645 | Expecting property name enclosed in double quotes: line 1 column 2 (char 1) |
| 646 | |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 647 | .. versionchanged:: 3.5 |
| 648 | The output is now in the same order as the input. Use the |
| 649 | :option:`--sort-keys` option to sort the output of dictionaries |
| 650 | alphabetically by key. |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 651 | |
| 652 | Command line options |
| 653 | ^^^^^^^^^^^^^^^^^^^^ |
| 654 | |
Benjamin Peterson | fc8e988 | 2014-04-13 19:52:14 -0400 | [diff] [blame] | 655 | .. cmdoption:: infile |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 656 | |
| 657 | The JSON file to be validated or pretty-printed:: |
| 658 | |
| 659 | $ python -m json.tool mp_films.json |
| 660 | [ |
| 661 | { |
| 662 | "title": "And Now for Something Completely Different", |
| 663 | "year": 1971 |
| 664 | }, |
| 665 | { |
| 666 | "title": "Monty Python and the Holy Grail", |
| 667 | "year": 1975 |
| 668 | } |
| 669 | ] |
| 670 | |
Benjamin Peterson | fc8e988 | 2014-04-13 19:52:14 -0400 | [diff] [blame] | 671 | If *infile* is not specified, read from :attr:`sys.stdin`. |
| 672 | |
| 673 | .. cmdoption:: outfile |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 674 | |
| 675 | Write the output of the *infile* to the given *outfile*. Otherwise, write it |
| 676 | to :attr:`sys.stdout`. |
| 677 | |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 678 | .. cmdoption:: --sort-keys |
| 679 | |
| 680 | Sort the output of dictionaries alphabetically by key. |
| 681 | |
| 682 | .. versionadded:: 3.5 |
| 683 | |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 684 | .. cmdoption:: -h, --help |
| 685 | |
| 686 | Show the help message. |
Serhiy Storchaka | 715f01b | 2014-11-27 19:45:31 +0200 | [diff] [blame] | 687 | |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 688 | |
| 689 | .. rubric:: Footnotes |
| 690 | |
| 691 | .. [#rfc-errata] As noted in `the errata for RFC 7159 |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 692 | <https://www.rfc-editor.org/errata_search.php?rfc=7159>`_, |
Serhiy Storchaka | 7a6915e | 2014-11-27 19:41:47 +0200 | [diff] [blame] | 693 | JSON permits literal U+2028 (LINE SEPARATOR) and |
| 694 | U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript |
| 695 | (as of ECMAScript Edition 5.1) does not. |