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
🌐
ReqBin
reqbin.com › code › python › c6fxmvxt › python-dump-vs-dumps-example
What is the difference between json.dump() vs json.dumps() in Python?
By default, json.dump() and json.dumps() generate minified versions of JSON to reduce the size of the file on disk and the size of data transmitted over the network. To get pretty (human-readable) JSON, you can pass the indent and sort_keys parameters to the json.dumps() and json.dump() methods. In this json.dumps() example, we are converting a Python object to JSON and pretty-print it.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-difference-between-json-dump-and-json-dumps
Difference between json.dump() and json.dumps() - Python - GeeksforGeeks
July 3, 2025 - json.dump(dict, file_pointer) Parameters: dictionary: name of dictionary which should be converted to JSON object. file pointer: pointer of the file opened in write or append mode. Example: Python ·
🌐
Medium
medium.com › snowflake › json-methods-load-vs-loads-and-dump-vs-dumps-21434a520b17
JSON Methods: load vs loads() and dump vs dumps() | by Sachin Mittal | Snowflake Builders Blog: Data Engineers, App Developers, AI, & Data Science | Medium
May 2, 2022 - Json.dump(): Used for writing the Python object i.e. dict to JSON file. ... For example, you have data in a list or dictionary or any Python object, and you want to encode and store it in a file in the form of JSON.
🌐
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?
🌐
GeeksforGeeks
geeksforgeeks.org › python-difference-between-json-dump-and-json-dumps
Python – Difference between json.dump() and json.dumps() | GeeksforGeeks
April 28, 2025 - Syntax: json.dumps(dict, indent) Parameters: dictionary – name of dictionary which should be converted to JSON object. indent – defines the number of units for indentation · Example: Python3 ·
🌐
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 - For example, you receive an HTTP request to send developer detail. you fetched developer data from the database table and store it in a Python dictionary or any Python object, Now you need to send that data back to the requested application so you need to convert the Python dictionary object into a JSON formatted string to send as a response in JSON string. To do this you need to use json.dumps().
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › json-dump-in-python
json.dump() in Python - GeeksforGeeks
January 8, 2020 - Example: This example shows how ... as f: json.dump(data, f) Output · Output · Explanation: json.dump(data, f) converts the dictionary data into JSON format....
🌐
Vitoshacademy
vitoshacademy.com › json-dump-vs-json-dumps-in-python
Json.dump vs json.dumps in Python – Useful code
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 ·
🌐
Educative
educative.io › answers › what-is-the-difference-between-jsonloads-and-jsondumps
What is the difference between json.loads() and json.dumps()?
The json.loads() takes in a string and returns a python object and the json.dumps() takes in a Python object and returns a JSON string.
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
Example 4: This example shows how json.dumps() converts a Python list into a JSON-formatted string, which is commonly used when sending list data through APIs.
Published   March 3, 2020
🌐
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 - If we don’t have to use the JSON string as JSON, we can also modify the separators to whatever we want. For example, we can let it become the PHP style as follows. json.dumps( my_dict, separators=('', ' => '), indent=2 )
🌐
iO Flood
ioflood.com › blog › python-json-dumps
Python json.dumps() | Step-by-Step Guide
February 1, 2024 - While json.dumps() works seamlessly with simple Python objects like dictionaries, its true strength lies in handling more complex structures. It can handle lists, nested dictionaries, and other complex Python objects with ease. Let’s illustrate this with an example.
🌐
Analytics Steps
analyticssteps.com › blogs › working-python-json-object
Working With Python JSON Objects | Analytics Steps
Here, we have used the file input output operation to create a new JSON file with the name “Data_File.json”. This file will be used to store a python dictionary named my_details as a JSON string, when we hit the json.dump() method.
🌐
GitHub
gist.github.com › code-review-doctor › b457f8e9020124cdd294f0bdf443deb9
Writing JSON files json.dumps vs json.dump · GitHub
Writing JSON files json.dumps vs json.dump · Raw · used-json-dump.csv · We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Analytics Vidhya
analyticsvidhya.com › home › python json.loads() and json.dump() methods
Python json.loads() and json.dump() methods - Analytics Vidhya
May 1, 2025 - The json.dump() function automatically ... types. For example, Python strings are converted to JSON strings, Python integers or floats are converted to JSON numbers, and Python lists are converted to JSON arrays....
🌐
BlogsHub
blogshub.co.in › home › understanding the difference between json.dump() and json.dumps() in python
BlogsHub Understanding the Difference Between json.dump() and json.dumps() in Python ,python dump vs dumps example
December 23, 2024 - Example: import json class CustomObject: def __init__(self, value): self.value = value obj = CustomObject(42) # Custom encoder function def custom_encoder(o): if isinstance(o, CustomObject): return {"CustomObjectValue": o.value} raise TypeError("Object not JSON serializable") # Serialize custom object json_string = json.dumps(obj, default=custom_encoder, indent=4) print(json_string) Understanding the difference between json.dump() and json.dumps() is crucial for working effectively with JSON in Python.