data is a Python dictionary. It needs to be encoded as JSON before writing.

Use this for maximum compatibility (Python 2 and 3):

import json
with open('data.json', 'w') as f:
    json.dump(data, f)

On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using:

import json
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

See json documentation.

Answer from phihag on Stack Overflow
Top answer
1 of 16
3348

data is a Python dictionary. It needs to be encoded as JSON before writing.

Use this for maximum compatibility (Python 2 and 3):

import json
with open('data.json', 'w') as f:
    json.dump(data, f)

On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using:

import json
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

See json documentation.

2 of 16
347

To get utf8-encoded file as opposed to ascii-encoded in the accepted answer for Python 2 use:

import io, json
with io.open('data.txt', 'w', encoding='utf-8') as f:
  f.write(json.dumps(data, ensure_ascii=False))

The code is simpler in Python 3:

import json
with open('data.txt', 'w') as f:
  json.dump(data, f, ensure_ascii=False)

On Windows, the encoding='utf-8' argument to open is still necessary.

To avoid storing an encoded copy of the data in memory (result of dumps) and to output utf8-encoded bytestrings in both Python 2 and 3, use:

import json, codecs
with open('data.txt', 'wb') as f:
    json.dump(data, codecs.getwriter('utf-8')(f), ensure_ascii=False)

The codecs.getwriter call is redundant in Python 3 but required for Python 2


Readability and size:

The use of ensure_ascii=False gives better readability and smaller size:

>>> json.dumps({'price': '€10'})
'{"price": "\\u20ac10"}'
>>> json.dumps({'price': '€10'}, ensure_ascii=False)
'{"price": "€10"}'

>>> len(json.dumps({'абвгд': 1}))
37
>>> len(json.dumps({'абвгд': 1}, ensure_ascii=False).encode('utf8'))
17

Further improve readability by adding flags indent=4, sort_keys=True (as suggested by dinos66) to arguments of dump or dumps. This way you'll get a nicely indented sorted structure in the json file at the cost of a slightly larger file size.

🌐
MLJAR
mljar.com › docs › python-write-json-to-file
Write JSON to file in Python
The above code saves dict object to file. The utf-8 encoding is used. Code recipes from Python cookbook.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-write-json-in-python
How do I write JSON in Python?
The function of json.dump() is to arrange a Python object into a JSON formatted stream into the specified file.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python - GeeksforGeeks
This process is called serialization. Let's explore two methods to write a JSON file in Python. The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object.
Published   August 5, 2025
🌐
freeCodeCamp
freecodecamp.org › news › python-read-json-file-how-to-load-json-from-a-file-and-parse-dumps
Python Read JSON File – How to Load JSON from a File and Parse Dumps
October 27, 2020 - But sometimes we might need to ... it, or work with it as a string. To do that, we can use the dumps function of the json module, passing the object as argument:...
🌐
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
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
Another variant of the dumps() function, called dump(), simply serializes the object to a text file. So if f is a text file object opened for writing, we can do this: ... To decode the object again, if f is a binary file or text file object ...
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python dump
Python Dump | How does the Python Dump Function Work?
June 16, 2023 - Then, we pass the dictionary variable to the json.dump function, which serializes the Python object and writes the JSON output to the pets_data file.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Better Stack
betterstack.com › community › questions › how-to-write-json-data-to-file-in-python
How do I write JSON data to a file in Python? | Better Stack Community
import json data = { "name": "John ... that contains the JSON data. The dump() function takes two arguments: the JSON data and the file-like object to which the data should be written....
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
2 weeks ago - Write the output of the infile to the given outfile. Otherwise, write it to sys.stdout. ... Sort the output of dictionaries alphabetically by key. Added in version 3.5. ... Disable escaping of non-ascii characters, see json.dumps() for more information.
🌐
GitHub
github.com › nlohmann › json
GitHub - nlohmann/json: JSON for Modern C++ · GitHub
In languages such as Python, JSON feels like a first-class data type. We used all the operator magic of modern C++ to achieve the same feeling in your code. Check out the examples below and you'll know what I mean. Trivial integration. Our whole code consists of a single header file json.hpp.
Starred by 49.1K users
Forked by 7.3K users
Languages   C++ 96.9% | CMake 2.0% | Python 0.6% | Makefile 0.3% | Starlark 0.1% | Jinja 0.1%
🌐
iO Flood
ioflood.com › blog › python-json-dumps
Python json.dumps() | Step-by-Step Guide
February 1, 2024 - The advantage of json.dump() is that it allows for efficient handling of large data. Instead of loading the entire JSON string into memory, it writes the data directly to a file.
🌐
Plus2Net
plus2net.com › python › json-dump.php
json.dump() to write Json output to file by using python file objects
import json data = {"name": "José", "country": "España"} with open("E:\\data.json", "w", encoding="utf-8") as file: json.dump(data, file, ensure_ascii=False) load() to read Json data from file · « Json & Python dumps() to convert to Json ...
🌐
Sabzlearn
sabzlearn.ir › qaa › python › how-do-i-write-json-data-to-a-file
https://sabzlearn.ir/qaa/python/how-do-i-write-jso...
What\'s the difference between a module and package in Python · How to split strings into words with multiple word boundary delimiters
🌐
ReqBin
reqbin.com › code › python › pbokf3iz › python-json-dumps-example
How to dump Python object to JSON using json.dumps()?
Python has a built-in module called json (JSON Encoder and Decoder) for working with JSON. To work with JSON in Python, you need to import the json module first. ... Serialization is the process of converting (encoding) Python objects into a stream of bytes (or string) to store the object in a database or file or transfer it over a network.
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - You write JSON with Python using json.dump() to serialize data to a file.
🌐
Its Linux FOSS
itslinuxfoss.com › home › python › how to write json to file in python?
How to Write JSON to File in Python? – Its Linux FOSS
January 29, 2023 - The “open()” function creates the file and opens it as in “w” mode. The “json.dump()” function is used to write Python objects into a file by taking the “value” as an argument.
🌐
Quora
quora.com › How-do-you-save-data-to-a-JSON-file-using-Python
How to save data to a JSON file using Python - Quora
Answer: Any dictionary can be written to an opened file directly using the json.dump() function (don’t forget to import json beforehand), passing the dictionary as first argument and the opened file handle as second argument.
🌐
Sentry
sentry.io › sentry answers › python › write json data to a file in python
Write JSON data to a file in Python | Sentry
We can do this using Python’s built-in json library and file operations. Specifically, the json.dump function allows us to serialize a Python dictionary as JSON for writing to disk.