What is the difference between json.dump() and json.dumps() in python?
There isn't much else to add other than what the docs say. If you want to dump the JSON into a file/socket or whatever, then you should go with dump()
. If you only need it as a string (for printing, parsing or whatever) then use dumps()
(dump string)
As mentioned by Antti Haapala in this answer, there are some minor differences on the ensure_ascii
behaviour. This is mostly due to how the underlying write()
function works, being that it operates on chunks rather than the whole string. Check his answer for more details on that.
json.dump()
Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object
If ensure_ascii is False, some chunks written to fp may be unicode instances
json.dumps()
Serialize obj to a JSON formatted str
If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance
The functions ending with s
accept string parameters. The other take file
streams or pointers to files.