How to Convert Dictionary to JSON in Python?
Hey Dev,
Today, I will let you know example of python dictionary convert to json. In this article, we will implement a how to convert dict to json in python. If you have a question about how to convert dictionary to json file in python then I will give a simple example with a solution. If you have a question about how to convert dictionary to json string in python then I will give a simple example with a solution. follow the below step for how to convert dictionary to json in python.
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.
So, without further ado, let's see simple examples:
You can use these examples with python3 (Python 3) version.
Example 1:
main.py
# Created New Dictionary myDictionary = { "id": 1, "name": "Hardik Savani", "email": "hardik@gmail.com" } # Dictionary convert to json jsonDictionary = json.dumps(myDictionary) print(jsonDictionary)
Output:
'{"id": 1, "name": "Hardik Savani", "email": "hardik@gmail.com"}'
Example 2:
main.py
# Created New Dictionary myDictionary = { "id": 1, "name": "Hardik Savani", "email": "hardik@gmail.com" } # 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
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert Dictionary Values into List in Python?
- How to Convert Dictionary to List in Python?
- How to Get Length of Dictionary Python?
- Python Dictionary Remove Duplicate Values Example
- Python Dictionary Check if Value is Empty Example
- How to Check if Python Dictionary is Empty or Not?
- Check if Key Value Pair Exists in Dictionary Python Example
- Python Dictionary Check if Value Exists Example
- Python Dictionary Check if Key Exists Example
- How to Update Value in Python Dictionary?
- How to Change Value in Python Dictionary?
- Python Dictionary Remove Element by Value Example
- Python Dictionary Delete Item by Key Example
- How to Remove Item from Dictionary in Python?