.dump()
StevenSwiniarski474 total contributions
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:
All contributors
- StevenSwiniarski474 total contributions
- Anonymous contributorAnonymous contributor95 total contributions
- StevenSwiniarski
- Anonymous contributor
Looking to contribute?
- 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.