Learn Python – How to read JSON file in Python- Basic and advance

JSON stands for JavaScript Object Notation, which is a famous information format to signify the structured data. It is an effective way to transmit the data between the server and web-applications.

The data representation in JSON is similar to the Python dictionary. The instance is given below.

Example –

{    
 "book": [    
  {     
       "id": 01,    
"language": "English",    
"edition": "Second",    
"author": "Derrick Mwiti"     
],    
   {    
  {     
    "id": 02,    
"language": "French",    
"edition": "Third",    
"author": "Vladimir"     
}    
}    

Read JSON file

First, we need to import the json module, and it affords the load() characteristic to study the JSON file.

Suppose, we have a JSON file named student.json, which includes JSON objects.

{"name": "Peter",   
"Subjects": ["English", "Political Science"]  
}  

Let’s understand the following example.

import json  
  
with open(r'C:\Users\DEVANSH SHARMA\student.json') as f:  
  data = json.load(f)  
print(data)  

Output:

{"name": "Peter", "Subjects": ["English", "Political Science"]}

Explanation –

In the above code, we have used the open() feature to read the JSON file. The load() function is parsed the JSON file and lower back the dictionary named data.