Welcome folks today in this blog post we will be creating json file and saving it using json module
. In this we will be converting python objects (Dictionairies) to JSON Objects. All the source code of the tutorial is shown below.
Get Started
In order to get started we need to install this following library called json
by pip command
pip install json
Now create a app.py
file inside the root directory and copy paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import json # python object(dictionary) to be dumped dict1 ={ "emp1": { "name": "Lisa", "designation": "programmer", "age": "34", "salary": "54000" }, "emp2": { "name": "Elis", "designation": "Trainee", "age": "24", "salary": "40000" }, } # the json file where the output must be stored out_file = open("myfile.json", "w") json.dump(dict1, out_file, indent = 6) out_file.close() |
Now if you execute this python script app.py
by executing command
python app.py