.dump()
Published May 4, 2022Updated Jun 15, 2022
Contribute to Docs
The .dump()
function encodes a Python object as a JSON file. The encoding conversion is based on the following table. The .dumps()
function, alternatively, takes a Python object and returns a JSON string.
Syntax
json.dump(py_obj, file_name)
json.dumps(py_obj)
A py_obj
can be a string, list, dictionary, etc.
Example
The .dump()
function can be used to convert a Python object to a JSON file:
import jsonimport stringletters = string.ascii_lowercasewith open('alphabet.json', 'w') as outfile:json.dump(letters, outfile)
Codebyte Example
The json
library can be used to encode and decode a Python dictionary:
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 Friendly90 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