json.dumps()¶
-
json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, sort_keys=False, **kw)[source]¶ Serialize
objto a JSON formattedstr.If
skipkeysis true thendictkeys that are not basic types (str,unicode,int,long,float,bool,None) will be skipped instead of raising aTypeError.If
ensure_asciiis false, all non-ASCII characters are not escaped, and the return value may be aunicodeinstance. Seedumpfor details.If
check_circularis false, then the circular reference check for container types will be skipped and a circular reference will result in anOverflowError(or worse).If
allow_nanis false, then it will be aValueErrorto serialize out of rangefloatvalues (nan,inf,-inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN,Infinity,-Infinity).If
indentis a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines.Noneis the most compact representation. Since the default item separator is', ', the output might include trailing whitespace whenindentis specified. You can useseparators=(',', ': ')to avoid this.If
separatorsis an(item_separator, dict_separator)tuple then it will be used instead of the default(', ', ': ')separators.(',', ':')is the most compact JSON representation.encodingis the character encoding for str instances, default is UTF-8.default(obj)is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.If sort_keys is
True(default:False), then the output of dictionaries will be sorted by key.To use a custom
JSONEncodersubclass (e.g. one that overrides the.default()method to serialize additional types), specify it with theclskwarg; otherwiseJSONEncoderis used.