Python Read a JSON File Tutorial

Published On: 13/01/2023 | Category: Python


Hi Dev,

This post will give you example of how to fetch data from json file in python. you will learn python read json file example. you will learn how to read json file in python. We will use python get json file content.

So, in this example we need to create a data.json file with some dummy data. then we read the file using open() and JSONload() function so follow this tutorial how we can read the json file in python.

So let's see bellow example:

Example

First of all we need to create data.json file.

data.json
[
  {
    "ID": 1,
    "Name": "Bhavesh Sonagra",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Nikhil Patel",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Piyush Kamani",
    "email": "[email protected]"
  }
]
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": "Bhavesh Sonagra", "email": "[email protected]"},
{ "ID": 2, "Name": "Nikhil Patel", "email": "[email protected]"},
{ "ID": 3, "Name": "Piyush Kamani", "email": "[email protected]"}

It will help you....