On this page
json.dump
Up: json | Next: json.dumps
Rel: json.dumps, json.JSONDecodeError, json.JSONDecoder, json.JSONEncoder
Ref: docs.python.org — json.dump
Tags: #public #python3 #stdlib #json
Import
import json
from json import dump
Signature & Overview
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)
Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object).
Usage
import io
import json
buf = io.StringIO()
json.dump({"name": "Ada", "active": True}, buf, indent=2, sort_keys=True)
assert '"active": true' in buf.getvalue()
Gotchas
- Writes to a text file-like object: Open files in
"w"mode withencoding="utf-8", not binary"wb"mode. - Use
dumps()for strings:dump(obj, fp)returnsNoneand writes directly tofp;dumps(obj)returns astr. - Non-JSON types raise
TypeError: Passdefault=str(or a custom serializer) forPath,datetime,Decimal, orset.
REPL Inspect
from json import dump
help(dump)
Linked from
Related notes
- tuts/python3/stdlib_module/json/dumps Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONDecodeError Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONDecoder Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/JSONEncoder Linked here; links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/json/load links to this page; shared tags: #json, #stdlib
- tuts/python3/stdlib_module/json/loads links to this page; shared tags: #json, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib