How to Read a JSON File in Python?

By Hardik Savani October 30, 2023 Category : Python

Hi Friends,

Today, how to read json file in python is our main topic. we will help you to give an example of how to get json file data in python. you will learn how to fetch data from json file in python. step by step explain python read json file example. So, let us see in detail an example.

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:

You can use these examples with python3 (Python 3) version.

Example 1:

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

data.json

[
  {
    "ID": 1,
    "Name": "Hardik Savani",
    "email": "hardik@gmail.com"
  },
  {
    "ID": 2,
    "Name": "Vimal Kashiyani",
    "email": "vimal@gmail.com"
  },
  {
    "ID": 3,
    "Name": "Harshad Pathak",
    "email": "harshad@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': 'Hardik Savani', 'email': 'hardik@gmail.com'}
{'ID': 2, 'Name': 'Vimal Kashiyani', 'email': 'vimal@gmail.com'}
{'ID': 3, 'Name': 'Harshad Pathak', 'email': 'harshad@gmail.com'}

I hope it can help you...

Tags :
Shares