Primary
json.dump() ⚬|Documentation|1st|20260208195155-00-⌔
json — JSON encoder and decoder — Python 3.14.3 documentation#json.dump
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) using this Python-to-JSON conversion table.Note: Unlike
pickleandmarshal, JSON is not a framed protocol, so trying to serialize multiple objects with repeated calls todump()using the same fp will result in an invalid JSON file.Parameters:
- obj (object) – The Python object to be serialized.
- fp (file-like object) – The file-like object obj will be serialized to. The
jsonmodule always producesstrobjects, notbytesobjects, thereforefp.write()must supportstrinput.- skipkeys (bool) – If
True, keys that are not of a basic type (str,int,float,bool,None) will be skipped instead of raising aTypeError. DefaultFalse.- ensure_ascii (bool) – If
True(the default), the output is guaranteed to have all incoming non-ASCII and non-printable characters escaped. IfFalse, all characters will be outputted as-is, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters U+0000 through U+001F.- check_circular (bool) – If
False, the circular reference check for container types is skipped and a circular reference will result in aRecursionError(or worse). DefaultTrue.- allow_nan (bool) – If
False, serialization of out-of-rangefloatvalues (nan,inf,-inf) will result in aValueError, in strict compliance with the JSON specification. IfTrue(the default), their JavaScript equivalents (NaN,Infinity,-Infinity) are used.- cls (a
JSONEncodersubclass) – If set, a custom JSON encoder with thedefault()method overridden, for serializing into custom datatypes. IfNone(the default),JSONEncoderis used.- indent (int * | * str * | * None) – If a positive integer or string, JSON array elements and object members will be pretty-printed with that indent level. A positive integer indents that many spaces per level; a string (such as
"\t") is used to indent each level. If zero, negative, or""(the empty string), only newlines are inserted. IfNone(the default), no newlines are inserted.- separators (tuple * | * None) – A two-tuple:
(item_separator, key_separator). IfNone(the default), separators defaults to(',', ':')if indent isNone, and(',', ':')otherwise. For the most compact JSON, specify(',', ':')to eliminate whitespace.- default (callable | None) – A function that is called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError. IfNone(the default),TypeErroris raised.- sort_keys (bool) – If
True, dictionaries will be outputted sorted by key. DefaultFalse.Changed in version 3.2: Allow strings for indent in addition to integers.
Changed in version 3.4: Use
(',', ':')as default if indent is notNone.Changed in version 3.6: All optional parameters are now keyword-only.
Printed 2026-06-28.
(echo:: @ ᯤ)
Link to original
Secondary
• • •