๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
The json.dumps() function in Python converts a Python object (such as a dictionary or list) into a JSON-formatted string. It is mainly used when you need to send data over APIs, store structured data or serialize Python objects into JSON text.
Published: January 13, 2026
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-dump-in-python
json.dump() in Python - GeeksforGeeks
January 13, 2026 - Example: This example shows how to write a Python dictionary into a JSON file using json.dump(). ... The JSON output is written directly into the file data.json. json.dump(obj, file, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None)
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
9
October 15, 2020
Python dumps "\n" instead of a newline in a json file - Stack Overflow
I have been taking some data through the Graph API of facebook and saving it in the json format in a new file. However, whenever I try to save it in the file, the new lines don't actually show as More on stackoverflow.com
๐ŸŒ stackoverflow.com
python 3.x - why json.dumps add \n in the output,how should I remove it while saving it in a file? - Stack Overflow
Why does json.dumps add \n in the output, and how should I remove it while saving it in a file? Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", " More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python json.dumps() outputs all my data into one line but I want to have a new line for each entry - Stack Overflow
I am working with Python and some json data. I am looping through my data (which are all dictionaries) and when I print the loop values to my console, I get 1 dictionary per line. However, when I do the same line of code with json.dumps() to convert my object into a string to be able to be output, I get multiple lines within the dictionary versus wanting the new ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.7 documentation
Unlike pickle and marshal, JSON is not a framed protocol, so trying to serialize multiple objects with repeated calls to dump() using the same fp will result in an invalid JSON file.
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ learn โ€บ courses โ€บ python json โ€บ json.dumps()
json.dumps() โ€“ Python JSON | Coddy
September 11, 2024 - It converts (serializes) a Python object into a JSON-formatted string. ... The function takes a Python object (like a dictionary or a list) as input and returns a JSON-formatted string.
๐ŸŒ
Medium
medium.com โ€บ @hemrajsaini1998 โ€บ python-advance-json-in-python-dump-dumps-load-and-loads-41ab0e7576fc
Python Advance: (JSON in Python) dump, dumps, load, and loads | by Hemraj Saini | Medium
July 27, 2025 - Think of this as dump to file. ... If you want to read a JSON file and convert it to a Python object, use json.load(). with open('output.json', 'r') as f: content = json.load(f) print(content['name']) # Output: Bob
๐ŸŒ
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?
Find elsewhere
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-json-dumps
Python json.dumps() | Step-by-Step Guide
February 1, 2024 - The json.dumps() function in Python is a part of the json module, which provides a method to convert Python objects into their JSON string representation. Letโ€™s break down its basic usage with a simple Python object โ€“ a dictionary. import ...
๐ŸŒ
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 ... 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()....
๐ŸŒ
Leapcell
leapcell.io โ€บ blog โ€บ understanding-json-dumps-in-python
Understanding `json.dumps()` in Python | Leapcell
July 25, 2025 - The json.dumps() function in Python is used to convert a Python object into a JSON-formatted string. The โ€œsโ€ in dumps stands for โ€œdump string.โ€ Unlike json.dump() which writes JSON data directly to a file, dumps() returns the JSON data ...
๐ŸŒ
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 - Python has built-in support to JSON documents, by its "json" module. I bet most of us have used it, and some of us have used it a lot. We know that we can use the json.dumps() method to easily convert a Python dictionary object into a JSON string. However, this method is not that simple as ...
๐ŸŒ
Amjadmajid
amjadmajid.github.io โ€บ tutorials โ€บ JSON_Python.html
Using JSON with Python
Python data type <-json.dumps(JSON sting) In [41]: integers = (1,2,3,4) # Python tuple numbers = json.dumps(integers) # Python tuple -> JSON string printStr(type(numbers), numbers) integers_2 = json.loads(numbers) # JSON string -> to what Python predicts (tuple of list) printStr(type(integers_2), integers_2) printStr(type(integers_2[0]), integers_2[0]) Type: <class 'str'> Content: [1, 2, 3, 4] Type: <class 'list'> Content: [1, 2, 3, 4] Type: <class 'int'> Content: 1 ยท
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
Use the indent parameter to define the numbers of indents: ... You can also define the separators, default value is (", ", ": "), which means using a comma and a space to separate each object, and a colon and a space to separate keys from values: Use the separators parameter to change the default separator: json.dumps(x, indent=4, separators=(".
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ python-json-dump
Python JSON Dump Method (with Examples)
September 8, 2023 - JSON (JavaScript Object Notation) is a widely used data interchange format for storing and exchanging data between a server and a client or between different parts of an application. In Python, the json module provides methods to work with JSON data. In this comprehensive guide, we will explore the 'json.dump()' method in depth, covering its usage, parameters, return values, exceptions, and more.
๐ŸŒ
Code-maven
python.code-maven.com โ€บ python-json โ€บ json โ€บ json-dumps.html
JSON dumps - Python JSON
import json data = { "fname" : 'Foo', "lname" : 'Bar', "email" : None, "children" : [ "Moo", "Koo", "Roo", ], "fixed": ("a", "b"), } print(data) json_str = json.dumps(data) print(json_str) with open('data.json', 'w') as fh: fh.write(json_str) {'fname': 'Foo', 'lname': 'Bar', 'email': None, 'children': ['Moo', 'Koo', 'Roo'], 'fixed': ('a', 'b')} {"fname": "Foo", "lname": "Bar", "email": null, "children": ["Moo", "Koo", "Roo"], "fixed": ["a", "b"]} dumps can be used to take a Python data structure and generate a string in JSON format.
๐ŸŒ
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.dumps() method can convert a Python object into a JSON string. 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: ...