.dump()
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. This function is essential for data persistence, APIs, and inter-process communication where data needs to be stored or transferred in a structured, language-independent format.
Syntax
json.dump(obj, file, *, skipkeys=False, ensure_ascii=True, indent=None, ...)
Parameters:
obj
: The Python object to be converted. It can be a string, list, dictionary, etc.file
: A file-like object opened in write mode ('w'
).skipkeys
(Optional): IfTrue
, skips dictionary keys that are not basic data types instead of raising aTypeError
.ensure_ascii
(Optional): IfTrue
, escapes all non-ASCII characters; set toFalse
for UTF-8 encoded output.indent
(Optional): If set to an integer (e.g.,2
or4
), pretty-prints the output with that many spaces of indentation.
Note: The ellipsis (
...
) in the syntax indicates that there can be additional optional parameters beyond those listed here.
Example 1: Basic Usage of .dump()
In this example, a Python dictionary is serialized into a JSON-formatted text file named person.json
using .dump()
:
import jsonperson = {'name': 'Alice','age': 30,'city': 'New York'}with open('person.json', 'w') as f:json.dump(person, f)
Example 2: .dump()
with Indentation and Non-ASCII Characters
This example writes a dictionary containing non-ASCII characters to a file using .dump()
. The ensure_ascii=False
parameter ensures characters are saved in UTF-8 format without being escaped, while indent=4
makes the output nicely formatted for readability:
import jsonproduct = {'name': 'Café Latte','price': 3.5,'available': True,'tags': ['coffee', 'beverage', 'café']}with open('product.json', 'w') as f:json.dump(product, f, indent=4, ensure_ascii=False)
Example 3: Serializing a List of Dictionaries Using .dump()
This example shows how to use .dump()
to serialize more complex structures like a list of dictionaries into JSON format:
import jsonusers = [{'id': 1, 'name': 'Alice'},{'id': 2, 'name': 'Bob'}]with open('users.json', 'w') as f:json.dump(users, f, indent=2)
Frequently Asked Questions
1. What is the difference between .dump()
and .dumps()
in Python?
.dump()
writes serialized data to a file-like object..dumps()
returns the serialized data as a string.
2. Can I use .dump()
with custom objects?
Not directly. You’ll need to define a custom serialization function or use default=str
to handle non-serializable objects.
3. What happens if the file used in .dump()
does not exist?
If the file used in .dump()
is opened in write mode ('w'
), Python will create the file automatically. If the directory path doesn’t exist, it will raise a FileNotFoundError
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours