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

4

5

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.

Davon answered 7/3, 2021 at 22:51 Comment(0)
M
6

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.

Microdont answered 7/3, 2021 at 23:3 Comment(3)
is dump (or with open and dump) asynchronous or will the dump (or with open and dump) completely finish executing before moving onwards in the script?Allout
@Allout It's synchronous. You might encounter problems if you try to read the file immediately after writing to it (before closing it) since the buffer might not be flushed yet, but otherwise it's a blocking operation.Graphic
ok, that is what i was expecting. thanks <3Allout
A
0

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

Aboriginal answered 7/3, 2021 at 23:1 Comment(0)
L
0

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.

Lacerta answered 7/3, 2021 at 23:10 Comment(0)
E
0

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.

Elvia answered 18/6 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.