Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 741
Kumaran Avatar asked Oct 06 '22 13:10

Kumaran


People also ask

What is the difference between JSON dumps and JSON loads in Python?

loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.

What does Python JSON dumps do?

The json. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json.

What is JSON dumps () method?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.

What is JSON dumps and JSON loads?

json loads -> returns an object from a string representing a json object. json dumps -> returns a string representing a json object from an object. > json dumps -> returns a string representing a json object from a dict. That's close, but it doesn't have to be a dict you pass in to json.


4 Answers

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

like image 168
João Gonçalves Avatar answered Oct 22 '22 00:10

João Gonçalves


The functions ending with s accept string parameters. The other take file streams or pointers to files.

like image 41
Pratik Gujarathi Avatar answered Oct 22 '22 01:10

Pratik Gujarathi


In memory usage and speed.

When you call jsonstr = json.dumps(mydata) it first creates a full copy of your data in memory and only then you file.write(jsonstr) it to disk. So this is a faster method but can be a problem if you have a big piece of data to save.

When you call json.dump(mydata, file) -- without 's', new memory is not used, as the data is dumped by chunks. But the whole process is about 2 times slower.

Source: I checked the source code of json.dump() and json.dumps() and also tested both the variants measuring the time with time.time() and watching the memory usage in htop.

like image 44
JenyaKh Avatar answered Oct 22 '22 02:10

JenyaKh


One notable difference in Python 2 is that if you're using ensure_ascii=False, dump will properly write UTF-8 encoded data into the file (unless you used 8-bit strings with extended characters that are not UTF-8):

dumps on the other hand, with ensure_ascii=False can produce a str or unicode just depending on what types you used for strings:

Serialize obj to a JSON formatted str using this conversion table. If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance.

(emphasis mine). Note that it may still be a str instance as well.

Thus you cannot use its return value to save the structure into file without checking which format was returned and possibly playing with unicode.encode.

This of course is not valid concern in Python 3 any more, since there is no more this 8-bit/Unicode confusion.


As for load vs loads, load considers the whole file to be one JSON document, so you cannot use it to read multiple newline limited JSON documents from a single file.