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. Answer from MattR0se on reddit.com
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
2 weeks ago - Disable escaping of non-ascii characters, see json.dumps() for more information. Added in version 3.9. ... Parse every input line as separate JSON object.
Codereview
codereview.doctor › features › python › best-practice › write-json-file-json-dump
Use json.dump to write JSON to file best practice | codereview.doctor
json.dumps is for JSON serialising an object to a string, while json.dump (no "s") is for JSON serialising an object to a file.
Videos
18:27
The Python JSON library .dump() and .dumps() Methods and Possible ...
18:22
JSON Dumps Function in Detail - Part 2 - YouTube
07:02
How to Use json dumps and loads methods to write and read data ...
21:39
Python JSON Module Tutorial: load, loads, dump, dumps ...
09:07
How to use JSON in Python - Part 1 | json.dumps() | json.loads() ...
09:25
How to Read and Write JSON File in Python #json #python - YouTube
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.
Published January 13, 2026
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › json.dumps.html
json.dumps() — Python Standard Library
Serialize obj to a JSON formatted str. If skipkeys is true then dict keys that are not basic types (str, unicode, int, long, float, bool, None) will be skipped instead of raising a TypeError. If ensure_ascii is false, all non-ASCII characters are not escaped, and the return value may be a unicode instance. See dump ...
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.
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?
DataCamp
datacamp.com › tutorial › json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - This function is used to serialize a Python object into a JSON string. The dumps() function takes a single argument, the Python object, and returns a JSON string.
Perforce Support
portal.perforce.com › s › article › JSON-Difference-between-json-dump-and-json-dumps
Perforce Support Portal
Loading · ×Sorry to interrupt · Refresh
Proxy-Seller
proxy-seller.com › blog › how-to-work-with-json-dump-in-python-ultimate-guide
How to Use json.dump() in Python: Useful Guide
This guide explains how the json.dump() and json.dumps in Python operate — a key tool for storing structured data in JSON format. This approach is widely applied when working with configuration files, web scraping results, API responses, and ...
ReqBin
reqbin.com › code › python › pbokf3iz › python-json-dumps-example
How to dump Python object to JSON using json.dumps()?
To dump a Python object to JSON string, you can use the json.dumps() method of the built-in json module. The json.dump() paired method (without the "s") converts the Python object to JSON string and writes it to a file.
JSON for Modern C++
json.nlohmann.me › api › basic_json › dump
dump - JSON for Modern C++
#include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { // create JSON values json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hellö 😀!"; // call dump() std::cout << "objects:" << '\n' << j_object.dump() << "\n\n" << j_object.dump(-1) << "\n\n" << j_object.dump(0) << "\n\n" << j_object.dump(4) << "\n\n" << j_object.dump(1, '\t') << "\n\n"; std::cout << "arrays:" << '\n' << j_array.dump() << "\n\n" << j_array.dump(-1) << "\n\n" << j_array.dump(0) << "\n\n" << j_array.dump(4) << "\n\n" << j_array.dump(1, '\t') << "
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 ...
Vertabelo Academy
academy.vertabelo.com › course › python-json › writing-json-files › writing-to-json-file › jsondumps-options-the-indent
How to Read and Write JSON Files in Python | Learn Python | Vertabelo Academy
We have saved some information in a Python object named data. Using json.dumps(), convert it to a string. Set the indent option to 4.