Convert Dictionary to JSON in Python Tutorial Example
Development teams using “Convert Dictionary to JSON in Python Tutorial Example” in client or internal work can use this detailed guide to document project effort and hand-offs, then assess the record against delivered functionality, tests and agreed outcomes.
Validate current syntax, security guidance and supported versions through Python.org before using the example in production.
Hi Dev,
This article is focused on convert dictionary to json in python tutorial example. you can understand a concept of how to convert dictionary to json in python. Here you will learn how to convert dictionary to json string in python. This post will give you simple example of how to convert dictionary to json file in python. Alright, let’s dive into the steps.
There are several ways to convert python dictionary to json. we will use dumps() function of json library to convert dictionary to json string in python.
Example 1:
main.py
# Created New Dictionary
myDictionary = {
"id": 1,
"name": "Piyush Kamani",
"email": "[email protected]"
}
# Dictionary convert to json
jsonDictionary = json.dumps(myDictionary)
print(jsonDictionary) Output:
'{"id": 1, "name": "Piyush Kamani", "email": "[email protected]"}'Example 2:
main.py
# Created New Dictionary
myDictionary = {
"id": 1,
"name": "Piyush Kamani",
"email": "[email protected]"
}
# Dictionary convert to json file
with open("demo.json", "w") as outfile:
json.dump(myDictionary, outfile)
print("Created new demo.json file") Output:
Created new demo.json file
# Python
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?