Steve
Steve

Reputation: 83

Should I use JSON.dumpS with .write() function or json.dump() in Python

Is there a difference between

object = {1:"one", 2:"two", 3:"three"}
file.write(json.dumps(object))

and json.dump(object). If no, which one should I use? Which one is best practices? I heard they do the same thing and wants to use the first one over the latter in my code. I'm using this to write to a JSON file.

Upvotes: 5

Views: 5304

Answers (4)

In my experience json.dump can take orders of magnitude longer than json.dumps + f.write. json_str = json.dumps(data,)

took: 5.81s

with open('/tmp/jpg_coco.json', 'w') as jf:

jf.write(json_str)

took 0.29s

but:

json.dump(data, filename)

took: 223.60s

It could possibly depend on the writing media latency (e.g. network storage). When there is an overhead associated with each writing operation, it may make sense to first compose the string and then write it to disk at once.

Upvotes: 0

Anon Coward
Anon Coward

Reputation: 10823

For the most part, the two methods are equivalent, however there is one important difference. json.dump can iterate over your data and write it out the file as it iterates, where as json.dumps must return a complete string for you to write to the output:

import json
values = list(range(1000000))
with open("test.json", "w") as f:
    # Maximum resident set size (kbytes): 62960
    # f.write(json.dumps(values))
    # Maximum resident set size (kbytes): 46828
    json.dump(values, f)

In some extreme cases, this will cause more memory usage, which could cause problems if you're ever in a resource constrained situation.

Best to avoid the issue unless you have a compelling reason to use json.dumps to output to a file.

Upvotes: 0

Jan Pokorný
Jan Pokorný

Reputation: 1868

Dumping JSON directly (json.dump) writes the serialized output to the file "on the fly", as it is created. On the other hand, dumping to a string (json.dumps) and then writing the string to a file happens sequentially, so nothing is written to the file until the whole object is serialized in memory.

In practice, this makes very little difference for reasonably sized JSONs. Unless your JSONs are at least several megabytes and you care about the performance, use whatever makes the code cleaner.

Upvotes: 6

Aspect
Aspect

Reputation: 57

json.dump is more ergonomic and friendly to the eye, as it's shorter and it conveys its meaning better than json.dumps does.

Upvotes: 0

Related Questions