What is the difference between json.dump() and json.dumps() in python?
I searched in this official document to find difference between the json.dump() and json.dumps() in python. It is clear that they are related with file write option.
But what is the detailed difference between them and in what situations one has more advantage than other?
There isn't pretty much anything 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 for
dump(). If you only need it as a string (for printing, parsing or whatever) then use
dumps() (dump string)
As mentioned by Antii 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 into chunks rather than the whole string. Check the answer for more details on that.
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
★ Back to homepage or read more recommendations:
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