Read a JSON File in Python Tutorial Example

02-Jan-2023

.

Admin

Read a JSON File in Python Tutorial Example

Hi Dev,

This simple article demonstrates of read a json file in python tutorial example. if you want to see example of python get json file content then you are a right place. I explained simply about python read json file content. This article will give you simple example of python read json file example. you will do the following things for how to fetch data from json file in python.

In this example, we will create one data.json file with some JSON data. Then we will read that file and print in python. we will use open() and JSON load() function to fetch JSON file data. So, let's see a simple example with output:

Example :


I simply created data.json file with content as like below:

data.json

[

{

"ID": 1,

"Name": "Piyush Patel",

"email": "piyush@gmail.com"

},

{

"ID": 2,

"Name": "Savan Rathod",

"email": "savan@gmail.com"

},

{

"ID": 3,

"Name": "Raju Mehta",

"email": "raju@gmail.com"

}

]

main.py

import json

# Opening JSON file

f = open('data.json')

# Get JSON Data from Object

data = json.load(f)

# Get JSON Data ROW

for row in data:

print(row)

# Closing file

f.close()

Output:

{'ID': 1, 'Name': 'Piyush Patel', 'email': 'piyush@gmail.com'}

{'ID': 2, 'Name': 'Savan Rathod', 'email': 'savan@gmail.com'}

{'ID': 3, 'Name': 'Raju Mehta', 'email': 'raju@gmail.com'}

#Python