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

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


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.


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


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 jsonvalues = 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.