ItSolutionStuff.com

How to Read a JSON File in Python?

By Hardik Savani • October 30, 2023
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: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Read CSV File Specific Column Example

Read Now →

How to Get Column Names from CSV File in Python?

Read Now →

Python Read CSV File Without Header Example

Read Now →

How to Read a CSV File in Python?

Read Now →

How to Write CSV File in Python?

Read Now →

How to Add Header in CSV File using Python?

Read Now →

Python Write CSV File from List Example

Read Now →

How to Check If a List is Empty or Not in Python?

Read Now →

Python Create Text File If Not Exists Example

Read Now →

How to Add Element to a List in Python?

Read Now →

How to Get Min Value from Python List?

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

How to Add Hours to Date in Python?

Read Now →