How to Read and Write a JSON File in Python?

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


Hi Dev,

In this post, we will learn python open read write json file. This article goes in detailed on how to read and write a json file in python. if you want to see example of how to create and read a json file in python then you are a right place. if you want to see example of python reading and writing json file example then you are a right place.

Here, in this example i will describe you read json file, write json file and read and write json file with example.

So let's see bellow example:

Example 1: Read JSON File in Python

So, in this example simple created data.json file:

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

Example 2: Write JSON File in Python

main.py
import json
  
# Create List for write data into json file
data = [
    { "ID": 1, "Name": "Bhavesh Sonagra", "email": "[email protected]"},
    { "ID": 2, "Name": "Piyush Kamani", "email": "[email protected]"},
    { "ID": 3, "Name": "Nikhil Thumar", "email": "[email protected]"}
]
  
# 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
[
  {
    "ID": 1,
    "Name": "Bhavesh Sonagra",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Piyush Kamani",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Nikhil Thumar",
    "email": "[email protected]"
  }
]

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

data.json
[
  {
    "ID": 1,
    "Name": "Bhavesh Sonagra",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Piyush Kamani",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Nikhil Thumar",
    "email": "[email protected]"
  }
]
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": "Rajesh Lalvadiya",
        "email": "[email protected]"
    })
  
# Append new object to list data
data.append({
        "ID": 5,
        "Name": "Mehul Bagda",
        "email": "[email protected]"
    })
    
# Create new JSON file
with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)
  
# Closing file
f.close()
Output
[
  {
    "ID": 1,
    "Name": "Bhavesh Sonagra",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Piyush Kamani",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Nikhil Thumar",
    "email": "[email protected]"
  },
  {
    "ID": 4,
    "Name": "Rajesh Lalvadiya",
    "email": "[email protected]"
  },
  {
    "ID": 5,
    "Name": "Mehul Bagda",
    "email": "[email protected]"
  }
]

It will help you....