Skip to content

All topics  /  Python

How to Read and Write a JSON File in Python?

Python

Development teams using “How to Read and Write a JSON File in Python?” in client or internal work can use learn more here to document project effort and hand-offs, then assess the record against delivered functionality, tests and agreed outcomes.

Validate current syntax, security guidance and supported versions through Python.org before using the example in production.

Jan 06, 2023

Hi Dev,

Here, I will show you how to works how to read and write a json file in python. let’s discuss about python open read write json file. I would like to show you python reading and writing json file example. you'll learn how to create and read a json file in python.

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.

Example 1: Read JSON File in Python


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

data.json

[
  {
    "ID": 1,
    "Name": "Piyush Patel",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Savan Rathod",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Raju Mochi",
    "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': 'Piyush Patel', 'email': '[email protected]'}
{'ID': 2, 'Name': 'Savan Rathod', 'email': '[email protected]'}
{'ID': 3, 'Name': 'Raju Mochi', '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": "Piyush Patel", "email": "[email protected]"},
    { "ID": 2, "Name": "Savan Rathod", "email": "[email protected]"},
    { "ID": 3, "Name": "Raju Mochi", "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": "Piyush Patel",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Savan Rathod",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Raju Mochi",
    "email": "[email protected]"
  }
]

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": "Piyush Patel",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Savan Rathod",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Raju Mochi",
    "email": "[email protected]"
  }
]

main.py

# 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": "Rahul Shinde",
        "email": "[email protected]"
    })

  
# Append new object to list data
data.append({
        "ID": 5,
        "Name": "Jaydip Pathar",
        "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": "Piyush Patel",
    "email": "[email protected]"
  },
  {
    "ID": 2,
    "Name": "Savan Rathod",
    "email": "[email protected]"
  },
  {
    "ID": 3,
    "Name": "Raju mochi"
    "email": "[email protected]"
  },
  {
    "ID": 4,
    "Name": "Rahul Shinde",
    "email": "[email protected]"
  },
  {
    "ID": 5,
    "Name": "Jaydip Pathar",
    "email": "[email protected]"
  }
]