Python 3 Create and Save JSON File Using JSON Module | Converting Python Objects to JSON Objects Full Tutorial For Beginners

Python 3 Create and Save JSON File Using JSON Module | Converting Python Objects to JSON Objects Full Tutorial For Beginners

 

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

 

 

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()

 

READ  Python 3 Pandas Script to Add New Rows to Existing DataFrame Using Append() and Concat() Method Full Example Project For Beginners

 

 

Now if you execute this python script app.py by executing command

 

python app.py

 

 

 

 

 

Leave a Reply