ItSolutionStuff.com

Python Read and Write a JSON File Example

By Hardik Savani • October 30, 2023
Python

Hi Developer,

If you need to see an example of python read and write json file. I would like to show you python open read write json file. you'll learn how to read and write a json file in python. This tutorial will give you a simple example of how to create and read a json file in python. follow the below example for python reading and writing json file example.

Here, I will give the following three examples of reading and writing a JSON file in the python program.

1) Read JSON File in Python

2) Write JSON File in Python

4) Read and Write to Same JSON File in Python

Let's see those three examples:

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

Example 1: Read JSON File in Python

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'}

Example 2: Write JSON File in Python

main.py

import json
  
# Create List for write data into json file
data = [
    { "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"}
]
  
# Create Json file with list
with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)
  
print("New data.json file is created from list")

Output:

After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:

[
  {
    "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"
  }
]

Example 3: Read and Write to Same JSON File in Python

I simply created data.json file with content as like below showed you. we will open that file and read it, Then write some more content on it.

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
  
# Read Existing JSON File
with open('data.json') as f:
    data = json.load(f)
  
# Append new object to list data
data.append({
        "ID": 4,
        "Name": "Paresh Patel",
        "email": "paresh@gmail.com"
    })
  
# Append new object to list data
data.append({
        "ID": 5,
        "Name": "Rakesh Patel",
        "email": "rakesh@gmail.com"
    })
    
# Create new JSON file
with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)
  
# Closing file
f.close()

Output:

After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:

[
  {
    "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"
  },
  {
    "ID": 4,
    "Name": "Paresh Patel",
    "email": "paresh@gmail.com"
  },
  {
    "ID": 5,
    "Name": "Rakesh Patel",
    "email": "rakesh@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 Create JSON File from List Example

Read Now →

How to Write a JSON File in Python?

Read Now →

How to Read a JSON File in Python?

Read Now →

How to Create a JSON File in Python?

Read Now →

Python Read CSV File Specific Column Example

Read Now →

How to Get Column Names from CSV File in Python?

Read Now →

How to Read a CSV File in Python?

Read Now →

How to Write Multiple Rows in CSV using Python?

Read Now →

How to Read Text File Line by Line in Python?

Read Now →

Python String Convert to Lowercase Example

Read Now →

Python Remove Empty String from List Example

Read Now →

How to Get First Element of List in Python?

Read Now →

Python Create Zip Archive from Directory Example

Read Now →