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

Answer from João Gonçalves on Stack Overflow
🌐
PYnative
pynative.com › home › python › json › python json dump() and dumps() for json encoding
Python JSON dump() and dumps() for JSON Encoding
May 14, 2021 - Understand various use of json.dump() and dumps(). Encode Python objects into JSON and Write it in a file.
Discussions

Why json.dump() and .load() are really needed?
JSON data in python is essentially a dictionary (at least they are interchangeable, there are some minor differences with the formatting). Have you tried saving a dictionary to a simple text file? my_data = { 'a': [1, 2, 3], 'b': {'foo': 'bar', 'baz': [4, 5, 6]} } with open('test_file.txt', 'w') as file: file.write(my_data) This is not possible because Python expects a string that it can write to a file. It doesn't know how to turn a dictionary into something it can write to a file. But, maybe you then do this instead: my_data = { "a": [1, 2, 3], "b": {"foo": "bar", "baz": [4, 5, 6]} } with open('test_file.txt', 'w') as file: # cast my_data to a string first file.write(str(my_data)) And it works. But what if you want to read that file? with open('test_file.txt', 'r') as file: read_data = file.read() Now you have a problem, because your output is this string: "{'a': [1, 2, 3], 'b': {'foo': 'bar', 'baz': [4, 5, 6]}}" How do you convert a string into a dictionary? This here doesn't work: with open('test_file.txt', 'r') as file: read_data = dict(file.read()) Python by itself does not know how to convert a string into a dictionary. For that you need JSON. Also it makes sure that you meet all the conventions of the JSON format so that you can exchange data between different languages (e.g. from Python to JavaScript). The other thing is, if you have a .txt file, how would anybody know that this file contains a data structure without opening the file? If the file extension says "JSON" everybody knows how to interpret the data. Same with .XML, .HTML etc. More on reddit.com
🌐 r/learnpython
18
8
October 15, 2020
Python updated json file is full of backslashes and newline characters
I have a python script where I am updating a json file with some of the key values to a particular value, but whenever I update the json file and try to write it to a different blank json file by using this: file.write(… More on discuss.python.org
🌐 discuss.python.org
0
January 24, 2023
Explaining the utilization of JSON's dumps() method in Python - Python - Data Science Dojo Discussions
The json.dumps() function in Python is used to convert a dictionary object into a string in the JSON data format. This can be useful when you need to represent the data in the dictionary as a string for tasks such as parsing or printing. The function returns a JSON string representation of ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
July 26, 2022
python - What is the difference between json.dumps and json.load? - Stack Overflow
What is the difference between json.dumps and json.load? From my understanding, one loads JSON into a dictionary and another loads into objects. More on stackoverflow.com
🌐 stackoverflow.com
🌐
ReqBin
reqbin.com › code › python › c6fxmvxt › python-dump-vs-dumps-example
What is the difference between json.dump() vs ...
July 27, 2023 - The json.dump() method converts a Python object into a JSON and writes it to a file, while the json.dumps() method encodes a Python object into JSON and returns a string. By default, json.dump() and json.dumps() generate minified versions of ...
🌐
Vitoshacademy
vitoshacademy.com › json-dump-vs-json-dumps-in-python
Json.dump vs json.dumps in Python – Useful code
sort_keys = True – sorting the beautiful JSON string · ensure_ascii · Python · 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: lines.py ·
🌐
Towards Data Science
towardsdatascience.com › home › latest › you must know python json dumps, but maybe not all aspects
You Must Know Python JSON Dumps, But Maybe Not All Aspects | Towards Data Science
January 20, 2025 - However, if we don’t want to ... false. ... The only difference is that the latter will really try to dump the circular referenced dictionary level by level until it goes overflow....
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › why json.dump() and .load() are really needed?
r/learnpython on Reddit: Why json.dump() and .load() are really needed?
October 15, 2020 -

Hi, hope everyone is well.

Just nearing the basics end of PCC book, I'm at saving user's data now. What exactly is the reason, when storing simple data, to use json.dump() or load(), instead of just saving and then reading it from simple text file?

I just can't place it in my head why do I really need it and it always makes it more difficult for me to learn if that's the case.

Thank you all in advance.

Top answer
1 of 8
6
JSON data in python is essentially a dictionary (at least they are interchangeable, there are some minor differences with the formatting). Have you tried saving a dictionary to a simple text file? my_data = { 'a': [1, 2, 3], 'b': {'foo': 'bar', 'baz': [4, 5, 6]} } with open('test_file.txt', 'w') as file: file.write(my_data) This is not possible because Python expects a string that it can write to a file. It doesn't know how to turn a dictionary into something it can write to a file. But, maybe you then do this instead: my_data = { "a": [1, 2, 3], "b": {"foo": "bar", "baz": [4, 5, 6]} } with open('test_file.txt', 'w') as file: # cast my_data to a string first file.write(str(my_data)) And it works. But what if you want to read that file? with open('test_file.txt', 'r') as file: read_data = file.read() Now you have a problem, because your output is this string: "{'a': [1, 2, 3], 'b': {'foo': 'bar', 'baz': [4, 5, 6]}}" How do you convert a string into a dictionary? This here doesn't work: with open('test_file.txt', 'r') as file: read_data = dict(file.read()) Python by itself does not know how to convert a string into a dictionary. For that you need JSON. Also it makes sure that you meet all the conventions of the JSON format so that you can exchange data between different languages (e.g. from Python to JavaScript). The other thing is, if you have a .txt file, how would anybody know that this file contains a data structure without opening the file? If the file extension says "JSON" everybody knows how to interpret the data. Same with .XML, .HTML etc.
2 of 8
3
How would you structure the text file so that you can load the data later?
🌐
Medium
pjcarroll.medium.com › json-dump-vs-json-dumps-json-load-vs-json-loads-b959f2647c9f
json.dump vs json.dumps, json.load vs json.loads | by PJ Carroll | Medium
December 27, 2020 - There it is: json.dump() sends data straight to a filehandler, json.dumps outputs it as a string.
🌐
Python.org
discuss.python.org › python help
Python updated json file is full of backslashes and newline characters - Python Help - Discussions on Python.org
January 24, 2023 - I have a python script where I am updating a json file with some of the key values to a particular value, but whenever I update the json file and try to write it to a different blank json file by using this: file.write(json.loads(json.dumps(replaced_content.replace("\\n", "")))) Result output json has a lot of slashes and new line characters "List" : true,\n "Name" : ""\n },\n "Config" : {\n "Range" : {\n "min" : "0.1"\n },\n "Box" : true,\n "Check" : {\n "Value" : ...
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
Explaining the utilization of JSON's dumps() method in Python - Python - Data Science Dojo Discussions
July 26, 2022 - The json.dumps() function in Python is used to convert a dictionary object into a string in the JSON data format. This can be useful when you need to represent the data in the dictionary as a string for tasks such as parsing or printing. The function returns a JSON string representation of the dictionary, which can be easily stored or transmitted.
🌐
FavTutor
favtutor.com › blogs › python-json-dump
Python JSON Dump Method (with Examples)
September 8, 2023 - The json.dump() method in Python is used for serializing Python objects into a JSON formatted stream, such as a file or a network socket, making it a key component in data serialization and interchange.
🌐
Board Infinity
boardinfinity.com › blog › json-dump
json.dumps in Python | Board Infinity
August 9, 2025 - json.dump is a function of Python which is used to convert a set of Python objects to a JSON string
🌐
Codecademy
codecademy.com › docs › python › json module › .dump()
Python | JSON Module | .dump() | Codecademy
May 29, 2025 - The .dump() function encodes a Python object as a JSON file. The process is known as serialization. The encoding conversion is based on this table.
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
3 weeks ago - Serialize obj to a JSON formatted str using this conversion table. The arguments have the same meaning as in dump(). ... Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings.
🌐
Programming-books
programming-books.io › essential › python › load-vs-loads-dump-vs-dumps-ef0cefb5a1ac4650ace119ea12c15b25
load vs loads dump vs dumps
As you can see the main difference is that when dumping json data you must pass the file handle to the function, as opposed to capturing the return value. Also worth noting is that you must seek to the start of the file before reading or writing, ...
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.