Tools, FAQ, Tutorials:
json.dumps() - Dumping Object into JSON
How to dump (or encode, serialize) a Python object into a JSON string using json.dumps()?
✍: FYIcenter.com
The json.dumps() function allows you to dump (or encode, serialize)
Python objects into JSON strings:
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)
The json.dumps() functions uses the following default encoding table:
Python Type JSON Type ----------- --------- dict object list array tuple array str string int number float number int- number float-derived Enums number True true False false None null
Some examples on using the json.dumps() function:
>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print(json.dumps("\"foo\bar"))
"\"foo\bar"
>>> print(json.dumps('\u1234'))
"\u1234"
>>> print(json.dumps('\\'))
"\\"
>>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
{"a": 0, "b": 0, "c": 0}
If you try to dump an object that is not supported with json.dumps(), you will get the "Object of type '...' is not JSON serializable" error:
>>> import json
>>> json.dumps(json)
Traceback (most recent call last):
File "C:...\Python36-32\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
...
TypeError: Object of type 'module' is not JSON serializable
⇒ Generating JSON Strings in Pretty Format
⇐ What Is Python Module 'json'
2018-10-13, ∼4244🔥, 0💬
Popular Posts:
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
What is EPUB 3.0 Metadata "dc:description" Element? EPUB 3.0 Metadata "dc:description" is an optiona...
Where to find tutorials on EPUB file format? I want to know how to create EPUB books. Here is a larg...
What Is session_register() in PHP? session_register() is old function that registers global variable...
What is EPUB 2.0 Metadata "dc:creator" and "dc:contributor" elements? EPUB 2.0 Metadata "dc:creator"...