JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript [1]).
Note: The term “object” in the context of JSON processing in Python can be ambiguous. All values in Python are objects. In JSON, an object refers to any data wrapped in curly braces, similar to a Python dictionary.
Warning: Be cautious when parsing JSON data from untrusted sources. A malicious JSON string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended.
This module exposes an API familiar to users of the standard library marshal and pickle modules.
>>> import json>>> class ComplexEncoder(json.JSONEncoder):... def default(self, obj):... if isinstance(obj, complex):... return [obj.real, obj.imag]... # Let the base class default method raise the TypeError... return super().default(obj)...>>> json.dumps(2 + 1j, cls=ComplexEncoder)'[2.0, 1.0]'>>> ComplexEncoder().encode(2 + 1j)'[2.0, 1.0]'>>> list(ComplexEncoder().iterencode(2 + 1j))['[2.0', ', 1.0', ']']
Using json from the shell to validate and pretty-print:
$ echo '{"json":"obj"}' | python -m json{ "json": "obj"}$ echo '{1.2:3.4}' | python -m jsonExpecting property name enclosed in double quotes: line 1 column 2 (char 1)
See Command-line interface for detailed documentation.
Note: JSON is a subset of YAML 1.2. The JSON produced by this module’s default settings (in particular, the default separators value) is also a subset of YAML 1.0 and 1.1. This module can thus also be used as a YAML serializer.
Note: This module’s encoders and decoders preserve input and output order by default. Order is only lost if the underlying containers are unordered.