Exchange structured application data
An HTTP client or server needs a portable payload representation.
Python · Module
Python’s json module encodes compatible Python data as JSON and decodes JSON back into Python objects. It supports strings, file-like objects, formatting options, hooks, and a command-line interface.
The module converts compatible Python object hierarchies to JSON text and reconstructs Python objects from JSON text. Use dumps and loads for strings, or dump and load with file-like objects; optional hooks and formatting arguments customize conversion.
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)| Name | Type | Description |
|---|---|---|
obj | object | Python object to serialize, for dump or dumps. |
fp | text file-like object | File-like destination with a write method, for dump. |
fp | text or binary file-like object | File-like source with a read method, for load. |
keyword options | keyword arguments | Optional formatting, conversion, ordering, and parsing controls such as indent, sort_keys, hooks, and parse functions. |
dumps returns a JSON-formatted string; loads and load return the corresponding Python object; dump writes JSON to the supplied file-like object.
import json
payload = {"name": "Ada", "roles": ["admin", "editor"]}
text = json.dumps(payload, indent=2, sort_keys=True)
print(text)
restored = json.loads(text)
assert restored == payloadSerialize a Python object to formatted JSON text, then parse that text back into a Python object.
An HTTP client or server needs a portable payload representation.
An application needs to persist simple configuration or state.
A developer needs to inspect or validate JSON outside application code.
Use json.dumps or json.dump to serialize Python data, and json.loads or json.load to deserialize it. The string variants operate on text, while the file variants use file-like objects.
Follow-up: How would you format deterministic, readable output? Pass sort_keys=True for key ordering and indent for human-readable indentation.
json.dumps returns a JSON-formatted string, whereas json.dump writes JSON to a file-like object. Conversely, json.loads parses a string and json.load reads from a file-like object.
Follow-up: What should be used when processing a stream incrementally? The documented load API expects a file-like object containing a JSON document; streaming protocol design requires handling framing separately.
The module provides a command-line interface through python -m json for validating and pretty-printing JSON objects. This is useful for checking or displaying JSON outside application code.
Follow-up: How can output be made easier to inspect? Use the command-line interface or supply indent when calling dumps or dump.
json.JSONEncoderjson.JSONDecoderjson.dumpjson.dumpsjson.loadjson.loadsjson.tool