Json.dump vs json.dumps in Python

The difference between json.dump and json.dumps is actually quite visible:

  • dump()  – dumps into a file or StringIO
  • dumps()  – dumps a string, that could be printed

Still, I am going to give a few example with these, as the dumps has a few nice built-in features:

    • ensure_ascii = False – allowing it to print even cyrillic
    • indent = 4  – making the JSON string look beautiful
    • sort_keys = True  – sorting the beautiful JSON string

The dump  (without “S”) can also ensure ascii, indent and sort the keys and this is the reason why I am not going to demonstrate it. Still, continuing the code from above, we may produce a nice file named “RealDeal.txt” with two lines in it, using the following code:

That’s all folks!

Tagged with: , , , ,