UserString.MutableString has been removed in Python 3.0.
[python.git] / Doc / library / json.rst
bloba0a62d1370189d1474d34d7ee0882baf392cd3da
1 :mod:`json` --- JSON encoder and decoder
2 ========================================
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>
8 .. versionadded:: 2.6
10 JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript
11 syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
13 :mod:`json` exposes an API familiar to users of the standard library
14 :mod:`marshal` and :mod:`pickle` modules.
16 Encoding basic Python object hierarchies::
17     
18     >>> 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"]'
35 Compact encoding::
37     >>> import json
38     >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
39     '[1,2,3,{"4":5,"6":7}]'
41 Pretty printing::
43     >>> import json
44     >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
45     {
46         "4": 5, 
47         "6": 7
48     }
50 Decoding JSON::
51     
52     >>> 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']
62 Specializing JSON object decoding::
64     >>> import json
65     >>> def as_complex(dct):
66     ...     if '__complex__' in dct:
67     ...         return complex(dct['real'], dct['imag'])
68     ...     return dct
69     ... 
70     >>> 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')
77 Extending :class:`JSONEncoder`::
78     
79     >>> 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)
85     ... 
86     >>> 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', ']']
92     
94 .. highlight:: none
96 Using json.tool from the shell to validate and pretty-print::
97     
98     $ 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)
105 .. highlight:: python
107 .. note:: 
109    The JSON produced by this module's default settings is a subset of
110    YAML, so it may be used as a serializer for that as well.
113 Basic Usage
114 -----------
116 .. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
118    Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
119    file-like object).
121    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`.
126    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.
132    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).
136    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``).
141    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.
146    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.
150    *encoding* is the character encoding for str instances, default is UTF-8.
152    *default(obj)* is a function that should return a serializable version of
153    *obj* or raise :exc:`TypeError`.  The default simply raises :exc:`TypeError`.
155    To use a custom :class:`JSONEncoder`` subclass (e.g. one that overrides the
156    :meth:`default` method to serialize additional types), specify it with the
157    *cls* kwarg.
160 .. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
162    Serialize *obj* to a JSON formatted :class:`str`.
164    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`.
169 .. function load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, **kw]]]]]]])
171    Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
172    document) to a Python object.
174    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`.
180    *object_hook* is an optional function that will be called with the result of
181    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).
185    *parse_float*, if specified, will be called with the string of every JSON
186    float to be decoded.  By default, this is equivalent to ``float(num_str)``.
187    This can be used to use another datatype or parser for JSON floats
188    (e.g. :class:`decimal.Decimal`).
190    *parse_int*, if specified, will be called with the string of every JSON int
191    to be decoded.  By default, this is equivalent to ``int(num_str)``.  This can
192    be used to use another datatype or parser for JSON integers
193    (e.g. :class:`float`).
195    *parse_constant*, if specified, will be called with one of the following
196    strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
197    ``'false'``.  This can be used to raise an exception if invalid JSON numbers
198    are encountered.
200    To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
201    kwarg.  Additional keyword arguments will be passed to the constructor of the
202    class.
205 .. function loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, **kw]]]]]]])
207    Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
208    document) to a Python object.
210    If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
211    other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
212    specified.  Encodings that are not ASCII based (such as UCS-2) are not
213    allowed and should be decoded to :class:`unicode` first.
215    The other arguments have the same meaning as in :func:`dump`.
218 Encoders and decoders
219 ---------------------
221 .. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict]]]]]])
223    Simple JSON decoder.
225    Performs the following translations in decoding by default:
227    +---------------+-------------------+
228    | JSON          | Python            |
229    +===============+===================+
230    | object        | dict              |
231    +---------------+-------------------+
232    | array         | list              |
233    +---------------+-------------------+
234    | string        | unicode           |
235    +---------------+-------------------+
236    | number (int)  | int, long         |
237    +---------------+-------------------+
238    | number (real) | float             |
239    +---------------+-------------------+
240    | true          | True              |
241    +---------------+-------------------+
242    | false         | False             |
243    +---------------+-------------------+
244    | null          | None              |
245    +---------------+-------------------+
247    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
248    corresponding ``float`` values, which is outside the JSON spec.
250    *encoding* determines the encoding used to interpret any :class:`str` objects
251    decoded by this instance (UTF-8 by default).  It has no effect when decoding
252    :class:`unicode` objects.
254    Note that currently only encodings that are a superset of ASCII work, strings
255    of other encodings should be passed in as :class:`unicode`.
257    *object_hook*, if specified, will be called with the result of every JSON
258    object decoded and its return value will be used in place of the given
259    :class:`dict`.  This can be used to provide custom deserializations (e.g. to
260    support JSON-RPC class hinting).
262    *parse_float*, if specified, will be called with the string of every JSON
263    float to be decoded.  By default, this is equivalent to ``float(num_str)``.
264    This can be used to use another datatype or parser for JSON floats
265    (e.g. :class:`decimal.Decimal`).
267    *parse_int*, if specified, will be called with the string of every JSON int
268    to be decoded.  By default, this is equivalent to ``int(num_str)``.  This can
269    be used to use another datatype or parser for JSON integers
270    (e.g. :class:`float`).
272    *parse_constant*, if specified, will be called with one of the following
273    strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
274    ``'false'``.  This can be used to raise an exception if invalid JSON numbers
275    are encountered.
278    .. method:: decode(s)
280       Return the Python representation of *s* (a :class:`str` or
281       :class:`unicode` instance containing a JSON document)
283    .. method:: raw_decode(s)
285       Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
286       beginning with a JSON document) and return a 2-tuple of the Python
287       representation and the index in *s* where the document ended.
289       This can be used to decode a JSON document from a string that may have
290       extraneous data at the end.
293 .. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
295    Extensible JSON encoder for Python data structures.
297    Supports the following objects and types by default:
299    +-------------------+---------------+
300    | Python            | JSON          |
301    +===================+===============+
302    | dict              | object        |
303    +-------------------+---------------+
304    | list, tuple       | array         |
305    +-------------------+---------------+
306    | str, unicode      | string        |
307    +-------------------+---------------+
308    | int, long, float  | number        |
309    +-------------------+---------------+
310    | True              | true          |
311    +-------------------+---------------+
312    | False             | false         |
313    +-------------------+---------------+
314    | None              | null          |
315    +-------------------+---------------+
317    To extend this to recognize other objects, subclass and implement a
318    :meth:`default` method with another method that returns a serializable object
319    for ``o`` if possible, otherwise it should call the superclass implementation
320    (to raise :exc:`TypeError`).
322    If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
323    attempt encoding of keys that are not str, int, long, float or None.  If
324    *skipkeys* is ``True``, such items are simply skipped.
326    If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be
327    :class:`str` objects with all incoming unicode characters escaped.  If
328    *ensure_ascii* is ``False``, the output will be a unicode object.
330    If *check_circular* is ``True`` (the default), then lists, dicts, and custom
331    encoded objects will be checked for circular references during encoding to
332    prevent an infinite recursion (which would cause an :exc:`OverflowError`).
333    Otherwise, no such check takes place.
335    If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
336    ``-Infinity`` will be encoded as such.  This behavior is not JSON
337    specification compliant, but is consistent with most JavaScript based
338    encoders and decoders.  Otherwise, it will be a :exc:`ValueError` to encode
339    such floats.
341    If *sort_keys* is ``True`` (the default), then the output of dictionaries
342    will be sorted by key; this is useful for regression tests to ensure that
343    JSON serializations can be compared on a day-to-day basis.
345    If *indent* is a non-negative integer (it is ``None`` by default), then JSON
346    array elements and object members will be pretty-printed with that indent
347    level.  An indent level of 0 will only insert newlines.  ``None`` is the most
348    compact representation.
350    If specified, *separators* should be an ``(item_separator, key_separator)``
351    tuple.  The default is ``(', ', ': ')``.  To get the most compact JSON
352    representation, you should specify ``(',', ':')`` to eliminate whitespace.
354    If specified, *default* is a function that gets called for objects that can't
355    otherwise be serialized.  It should return a JSON encodable version of the
356    object or raise a :exc:`TypeError`.
358    If *encoding* is not ``None``, then all input strings will be transformed
359    into unicode using that encoding prior to JSON-encoding.  The default is
360    UTF-8.
363    .. method:: default(o)
365       Implement this method in a subclass such that it returns a serializable
366       object for *o*, or calls the base implementation (to raise a
367       :exc:`TypeError`).
369       For example, to support arbitrary iterators, you could implement default
370       like this::
371             
372          def default(self, o):
373             try:
374                iterable = iter(o)
375             except TypeError:
376                pass
377             else:
378                 return list(iterable)
379             return JSONEncoder.default(self, o)
382    .. method:: encode(o)
384       Return a JSON string representation of a Python data structure, *o*.  For
385       example::
387         >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
388         '{"foo": ["bar", "baz"]}'
391    .. method:: iterencode(o)
393       Encode the given object, *o*, and yield each string representation as
394       available.  For example::
395             
396             for chunk in JSONEncoder().iterencode(bigobject):
397                 mysocket.write(chunk)