Python Glossary Русский

all Python 3jsonfunction

json.dump

json

Serialize an object as JSON and write it to a file object.

Syntax

json.dump(obj, fp)

dump emits text, so the file must be opened in text mode ('w'); a binary handle raises TypeError. With the default ensure_ascii=True every non-ASCII character is written as a \uXXXX escape — pass ensure_ascii=False and open the file with encoding='utf-8' to keep it readable. No trailing newline is written, which matters if you append several documents to one file.

Examples

import json, io
buf = io.StringIO()
json.dump({'a': 1}, buf)
print(buf.getvalue())   # → {"a": 1}

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy