This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to convert a dictionary to JSON in Python.
Let’s say you have a dictionary named ‘a’.
# Import JSON module
import json
# A dictionary named 'a'
a = {
"Name": "Meat Pizza",
"Price": "$12",
"Size": "Regular",
"Crust Type": "Deep Dish"
}
Convert Dictionary to JSON in Python
In order to convert a dictionary to JSON, you can use the json.dumps()
method.
# Import JSON module
import json
# A dictionary named 'a'
a = {
"Name": "Meat Pizza",
"Price": "$12",
"Size": "Regular",
"Crust Type": "Deep Dish"
}
# Convert dictionary named 'a' to JSON
print(json.dumps(a, indent = 4))
# {
# "Name": "Meat Pizza",
# "Price": "$12",
# "Size": "Regular",
# "Crust Type": "Deep Dish"
# }
Note: The json.dumps()
method functions by converting Python objects to JSON string.
The post How to Convert Dictionary to JSON in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-26T10:37:12+00:00) How to Convert Dictionary to JSON in Python. Retrieved from https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.