blob: 9eaa3b3ee5e8b1527bfd47faa00578ee9c3b314f [file] [log] [blame]
Georg Brandl3961f182008-05-05 20:53:39 +00001:mod:`json` --- JSON encoder and decoder
2========================================
Brett Cannon4b964f92008-05-05 20:21:38 +00003
4.. module:: json
Georg Brandl3961f182008-05-05 20:53:39 +00005 :synopsis: Encode and decode the JSON format.
Brett Cannon4b964f92008-05-05 20:21:38 +00006.. moduleauthor:: Bob Ippolito <bob@redivi.com>
7.. sectionauthor:: Bob Ippolito <bob@redivi.com>
8.. versionadded:: 2.6
9
10JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript
11syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
12
Georg Brandl3961f182008-05-05 20:53:39 +000013:mod:`json` exposes an API familiar to users of the standard library
14:mod:`marshal` and :mod:`pickle` modules.
Brett Cannon4b964f92008-05-05 20:21:38 +000015
16Encoding basic Python object hierarchies::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000017
Brett Cannon4b964f92008-05-05 20:21:38 +000018 >>> import json
19 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
20 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
21 >>> print json.dumps("\"foo\bar")
22 "\"foo\bar"
23 >>> print json.dumps(u'\u1234')
24 "\u1234"
25 >>> print json.dumps('\\')
26 "\\"
27 >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
28 {"a": 0, "b": 0, "c": 0}
29 >>> from StringIO import StringIO
30 >>> io = StringIO()
31 >>> json.dump(['streaming API'], io)
32 >>> io.getvalue()
33 '["streaming API"]'
34
35Compact encoding::
36
37 >>> import json
38 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
39 '[1,2,3,{"4":5,"6":7}]'
40
41Pretty printing::
42
43 >>> import json
44 >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
45 {
Georg Brandlc62ef8b2009-01-03 20:55:06 +000046 "4": 5,
Brett Cannon4b964f92008-05-05 20:21:38 +000047 "6": 7
48 }
49
50Decoding JSON::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000051
Brett Cannon4b964f92008-05-05 20:21:38 +000052 >>> import json
53 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
54 [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
55 >>> json.loads('"\\"foo\\bar"')
56 u'"foo\x08ar'
57 >>> from StringIO import StringIO
58 >>> io = StringIO('["streaming API"]')
59 >>> json.load(io)
60 [u'streaming API']
61
62Specializing JSON object decoding::
63
64 >>> import json
65 >>> def as_complex(dct):
66 ... if '__complex__' in dct:
67 ... return complex(dct['real'], dct['imag'])
68 ... return dct
Georg Brandlc62ef8b2009-01-03 20:55:06 +000069 ...
Brett Cannon4b964f92008-05-05 20:21:38 +000070 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
71 ... object_hook=as_complex)
72 (1+2j)
73 >>> import decimal
74 >>> json.loads('1.1', parse_float=decimal.Decimal)
75 Decimal('1.1')
76
Georg Brandl3961f182008-05-05 20:53:39 +000077Extending :class:`JSONEncoder`::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000078
Brett Cannon4b964f92008-05-05 20:21:38 +000079 >>> import json
80 >>> class ComplexEncoder(json.JSONEncoder):
81 ... def default(self, obj):
82 ... if isinstance(obj, complex):
83 ... return [obj.real, obj.imag]
84 ... return json.JSONEncoder.default(self, obj)
Georg Brandlc62ef8b2009-01-03 20:55:06 +000085 ...
Brett Cannon4b964f92008-05-05 20:21:38 +000086 >>> dumps(2 + 1j, cls=ComplexEncoder)
87 '[2.0, 1.0]'
88 >>> ComplexEncoder().encode(2 + 1j)
89 '[2.0, 1.0]'
90 >>> list(ComplexEncoder().iterencode(2 + 1j))
91 ['[', '2.0', ', ', '1.0', ']']
Georg Brandlc62ef8b2009-01-03 20:55:06 +000092
Brett Cannon4b964f92008-05-05 20:21:38 +000093
94.. highlight:: none
95
96Using json.tool from the shell to validate and pretty-print::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000097
Brett Cannon4b964f92008-05-05 20:21:38 +000098 $ echo '{"json":"obj"}' | python -mjson.tool
99 {
100 "json": "obj"
101 }
102 $ echo '{ 1.2:3.4}' | python -mjson.tool
103 Expecting property name: line 1 column 2 (char 2)
104
105.. highlight:: python
106
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000107.. note::
Brett Cannon4b964f92008-05-05 20:21:38 +0000108
Georg Brandl3961f182008-05-05 20:53:39 +0000109 The JSON produced by this module's default settings is a subset of
Brett Cannon4b964f92008-05-05 20:21:38 +0000110 YAML, so it may be used as a serializer for that as well.
111
112
113Basic Usage
114-----------
115
116.. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
117
Georg Brandl3961f182008-05-05 20:53:39 +0000118 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
119 file-like object).
Brett Cannon4b964f92008-05-05 20:21:38 +0000120
Georg Brandl3961f182008-05-05 20:53:39 +0000121 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
122 of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`,
123 :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
124 :exc:`TypeError`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000125
Georg Brandl3961f182008-05-05 20:53:39 +0000126 If *ensure_ascii* is ``False`` (default: ``True``), then some chunks written
127 to *fp* may be :class:`unicode` instances, subject to normal Python
128 :class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()``
129 explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this
130 is likely to cause an error.
Brett Cannon4b964f92008-05-05 20:21:38 +0000131
Georg Brandl3961f182008-05-05 20:53:39 +0000132 If *check_circular* is ``False`` (default: ``True``), then the circular
133 reference check for container types will be skipped and a circular reference
134 will result in an :exc:`OverflowError` (or worse).
Brett Cannon4b964f92008-05-05 20:21:38 +0000135
Georg Brandl3961f182008-05-05 20:53:39 +0000136 If *allow_nan* is ``False`` (default: ``True``), then it will be a
137 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
138 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
139 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
Brett Cannon4b964f92008-05-05 20:21:38 +0000140
Georg Brandl3961f182008-05-05 20:53:39 +0000141 If *indent* is a non-negative integer, then JSON array elements and object
142 members will be pretty-printed with that indent level. An indent level of 0
143 will only insert newlines. ``None`` (the default) selects the most compact
144 representation.
Brett Cannon4b964f92008-05-05 20:21:38 +0000145
Georg Brandl3961f182008-05-05 20:53:39 +0000146 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
147 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
148 ':')`` is the most compact JSON representation.
Brett Cannon4b964f92008-05-05 20:21:38 +0000149
Georg Brandl3961f182008-05-05 20:53:39 +0000150 *encoding* is the character encoding for str instances, default is UTF-8.
Brett Cannon4b964f92008-05-05 20:21:38 +0000151
Georg Brandl3961f182008-05-05 20:53:39 +0000152 *default(obj)* is a function that should return a serializable version of
153 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000154
Georg Brandlfc29f272009-01-02 20:25:14 +0000155 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Georg Brandl3961f182008-05-05 20:53:39 +0000156 :meth:`default` method to serialize additional types), specify it with the
157 *cls* kwarg.
Brett Cannon4b964f92008-05-05 20:21:38 +0000158
159
Georg Brandl3961f182008-05-05 20:53:39 +0000160.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000161
Georg Brandl3961f182008-05-05 20:53:39 +0000162 Serialize *obj* to a JSON formatted :class:`str`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000163
Georg Brandl3961f182008-05-05 20:53:39 +0000164 If *ensure_ascii* is ``False``, then the return value will be a
165 :class:`unicode` instance. The other arguments have the same meaning as in
166 :func:`dump`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000167
168
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000169.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000170
171 Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
172 document) to a Python object.
173
Georg Brandl3961f182008-05-05 20:53:39 +0000174 If the contents of *fp* are encoded with an ASCII based encoding other than
175 UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
176 Encodings that are not ASCII based (such as UCS-2) are not allowed, and
177 should be wrapped with ``codecs.getreader(fp)(encoding)``, or simply decoded
178 to a :class:`unicode` object and passed to :func:`loads`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000179
180 *object_hook* is an optional function that will be called with the result of
Georg Brandl3961f182008-05-05 20:53:39 +0000181 any object literal decode (a :class:`dict`). The return value of
182 *object_hook* will be used instead of the :class:`dict`. This feature can be used
183 to implement custom decoders (e.g. JSON-RPC class hinting).
184
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000185 *object_pairs_hook* is an optional function that will be called with the
186 result of any object literal decode with an ordered list of pairs. The
187 return value of *object_pairs_hook* will be used instead of the
188 :class:`dict`. This feature can be used to implement custom decoders that
189 rely on the order that the key and value pairs are decoded (for example,
190 :func:`collections.OrderedDict` will remember the order of insertion). If
191 *object_hook* is also defined, the *object_pairs_hook* takes priority.
192
193 .. versionchanged:: 2.7
194 Added support for *object_pairs_hook*.
195
Georg Brandl3961f182008-05-05 20:53:39 +0000196 *parse_float*, if specified, will be called with the string of every JSON
197 float to be decoded. By default, this is equivalent to ``float(num_str)``.
198 This can be used to use another datatype or parser for JSON floats
199 (e.g. :class:`decimal.Decimal`).
200
201 *parse_int*, if specified, will be called with the string of every JSON int
202 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
203 be used to use another datatype or parser for JSON integers
204 (e.g. :class:`float`).
205
206 *parse_constant*, if specified, will be called with one of the following
207 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
208 ``'false'``. This can be used to raise an exception if invalid JSON numbers
209 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000210
211 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandl3961f182008-05-05 20:53:39 +0000212 kwarg. Additional keyword arguments will be passed to the constructor of the
Brett Cannon4b964f92008-05-05 20:21:38 +0000213 class.
214
215
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000216.. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Georg Brandl3961f182008-05-05 20:53:39 +0000217
218 Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
219 document) to a Python object.
220
221 If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
222 other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
223 specified. Encodings that are not ASCII based (such as UCS-2) are not
224 allowed and should be decoded to :class:`unicode` first.
225
226 The other arguments have the same meaning as in :func:`dump`.
227
228
Brett Cannon4b964f92008-05-05 20:21:38 +0000229Encoders and decoders
230---------------------
231
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000232.. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict[, object_pairs_hook]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000233
Georg Brandl3961f182008-05-05 20:53:39 +0000234 Simple JSON decoder.
Brett Cannon4b964f92008-05-05 20:21:38 +0000235
236 Performs the following translations in decoding by default:
237
238 +---------------+-------------------+
239 | JSON | Python |
240 +===============+===================+
241 | object | dict |
242 +---------------+-------------------+
243 | array | list |
244 +---------------+-------------------+
245 | string | unicode |
246 +---------------+-------------------+
247 | number (int) | int, long |
248 +---------------+-------------------+
249 | number (real) | float |
250 +---------------+-------------------+
251 | true | True |
252 +---------------+-------------------+
253 | false | False |
254 +---------------+-------------------+
255 | null | None |
256 +---------------+-------------------+
257
258 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
259 corresponding ``float`` values, which is outside the JSON spec.
260
Georg Brandl3961f182008-05-05 20:53:39 +0000261 *encoding* determines the encoding used to interpret any :class:`str` objects
262 decoded by this instance (UTF-8 by default). It has no effect when decoding
263 :class:`unicode` objects.
Brett Cannon4b964f92008-05-05 20:21:38 +0000264
Georg Brandl3961f182008-05-05 20:53:39 +0000265 Note that currently only encodings that are a superset of ASCII work, strings
266 of other encodings should be passed in as :class:`unicode`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000267
268 *object_hook*, if specified, will be called with the result of every JSON
269 object decoded and its return value will be used in place of the given
Georg Brandl3961f182008-05-05 20:53:39 +0000270 :class:`dict`. This can be used to provide custom deserializations (e.g. to
Brett Cannon4b964f92008-05-05 20:21:38 +0000271 support JSON-RPC class hinting).
272
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000273 *object_pairs_hook*, if specified will be called with the result of every
274 JSON object decoded with an ordered list of pairs. The return value of
275 *object_pairs_hook* will be used instead of the :class:`dict`. This
276 feature can be used to implement custom decoders that rely on the order
277 that the key and value pairs are decoded (for example,
278 :func:`collections.OrderedDict` will remember the order of insertion). If
279 *object_hook* is also defined, the *object_pairs_hook* takes priority.
280
281 .. versionchanged:: 2.7
282 Added support for *object_pairs_hook*.
283
Brett Cannon4b964f92008-05-05 20:21:38 +0000284 *parse_float*, if specified, will be called with the string of every JSON
Georg Brandl3961f182008-05-05 20:53:39 +0000285 float to be decoded. By default, this is equivalent to ``float(num_str)``.
286 This can be used to use another datatype or parser for JSON floats
287 (e.g. :class:`decimal.Decimal`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000288
289 *parse_int*, if specified, will be called with the string of every JSON int
Georg Brandl3961f182008-05-05 20:53:39 +0000290 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
291 be used to use another datatype or parser for JSON integers
292 (e.g. :class:`float`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000293
294 *parse_constant*, if specified, will be called with one of the following
Georg Brandl3961f182008-05-05 20:53:39 +0000295 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
296 ``'false'``. This can be used to raise an exception if invalid JSON numbers
297 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000298
299
300 .. method:: decode(s)
301
Georg Brandl3961f182008-05-05 20:53:39 +0000302 Return the Python representation of *s* (a :class:`str` or
303 :class:`unicode` instance containing a JSON document)
Brett Cannon4b964f92008-05-05 20:21:38 +0000304
305 .. method:: raw_decode(s)
306
Georg Brandl3961f182008-05-05 20:53:39 +0000307 Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
308 beginning with a JSON document) and return a 2-tuple of the Python
309 representation and the index in *s* where the document ended.
Brett Cannon4b964f92008-05-05 20:21:38 +0000310
Georg Brandl3961f182008-05-05 20:53:39 +0000311 This can be used to decode a JSON document from a string that may have
312 extraneous data at the end.
Brett Cannon4b964f92008-05-05 20:21:38 +0000313
314
315.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
316
Georg Brandl3961f182008-05-05 20:53:39 +0000317 Extensible JSON encoder for Python data structures.
Brett Cannon4b964f92008-05-05 20:21:38 +0000318
319 Supports the following objects and types by default:
320
321 +-------------------+---------------+
322 | Python | JSON |
323 +===================+===============+
324 | dict | object |
325 +-------------------+---------------+
326 | list, tuple | array |
327 +-------------------+---------------+
328 | str, unicode | string |
329 +-------------------+---------------+
330 | int, long, float | number |
331 +-------------------+---------------+
332 | True | true |
333 +-------------------+---------------+
334 | False | false |
335 +-------------------+---------------+
336 | None | null |
337 +-------------------+---------------+
338
339 To extend this to recognize other objects, subclass and implement a
Georg Brandl3961f182008-05-05 20:53:39 +0000340 :meth:`default` method with another method that returns a serializable object
Brett Cannon4b964f92008-05-05 20:21:38 +0000341 for ``o`` if possible, otherwise it should call the superclass implementation
342 (to raise :exc:`TypeError`).
343
344 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
345 attempt encoding of keys that are not str, int, long, float or None. If
346 *skipkeys* is ``True``, such items are simply skipped.
347
Georg Brandl3961f182008-05-05 20:53:39 +0000348 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be
349 :class:`str` objects with all incoming unicode characters escaped. If
350 *ensure_ascii* is ``False``, the output will be a unicode object.
Brett Cannon4b964f92008-05-05 20:21:38 +0000351
352 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
353 encoded objects will be checked for circular references during encoding to
354 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
355 Otherwise, no such check takes place.
356
Georg Brandl3961f182008-05-05 20:53:39 +0000357 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
358 ``-Infinity`` will be encoded as such. This behavior is not JSON
359 specification compliant, but is consistent with most JavaScript based
360 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
361 such floats.
Brett Cannon4b964f92008-05-05 20:21:38 +0000362
363 If *sort_keys* is ``True`` (the default), then the output of dictionaries
364 will be sorted by key; this is useful for regression tests to ensure that
365 JSON serializations can be compared on a day-to-day basis.
366
Georg Brandl3961f182008-05-05 20:53:39 +0000367 If *indent* is a non-negative integer (it is ``None`` by default), then JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000368 array elements and object members will be pretty-printed with that indent
369 level. An indent level of 0 will only insert newlines. ``None`` is the most
370 compact representation.
371
Georg Brandl3961f182008-05-05 20:53:39 +0000372 If specified, *separators* should be an ``(item_separator, key_separator)``
373 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000374 representation, you should specify ``(',', ':')`` to eliminate whitespace.
375
376 If specified, *default* is a function that gets called for objects that can't
377 otherwise be serialized. It should return a JSON encodable version of the
378 object or raise a :exc:`TypeError`.
379
380 If *encoding* is not ``None``, then all input strings will be transformed
381 into unicode using that encoding prior to JSON-encoding. The default is
382 UTF-8.
383
384
385 .. method:: default(o)
386
387 Implement this method in a subclass such that it returns a serializable
388 object for *o*, or calls the base implementation (to raise a
389 :exc:`TypeError`).
390
391 For example, to support arbitrary iterators, you could implement default
392 like this::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000393
Brett Cannon4b964f92008-05-05 20:21:38 +0000394 def default(self, o):
395 try:
Georg Brandl1379ae02008-09-24 09:47:55 +0000396 iterable = iter(o)
Brett Cannon4b964f92008-05-05 20:21:38 +0000397 except TypeError:
Georg Brandl1379ae02008-09-24 09:47:55 +0000398 pass
Brett Cannon4b964f92008-05-05 20:21:38 +0000399 else:
400 return list(iterable)
401 return JSONEncoder.default(self, o)
402
403
404 .. method:: encode(o)
405
Georg Brandl3961f182008-05-05 20:53:39 +0000406 Return a JSON string representation of a Python data structure, *o*. For
Brett Cannon4b964f92008-05-05 20:21:38 +0000407 example::
408
409 >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
410 '{"foo": ["bar", "baz"]}'
411
412
413 .. method:: iterencode(o)
414
415 Encode the given object, *o*, and yield each string representation as
Georg Brandl3961f182008-05-05 20:53:39 +0000416 available. For example::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000417
Brett Cannon4b964f92008-05-05 20:21:38 +0000418 for chunk in JSONEncoder().iterencode(bigobject):
419 mysocket.write(chunk)