Read JSON file using Python (original) (raw)

Last Updated : 23 May, 2026

JSON stands for JavaScript Object Notation. It is a lightweight text format used to store and exchange structured data. Python provides a built-in json module to read and write this format.

Example: Reading JSON File using Python. We will be using Python’s json module, which offers several methods to work with JSON data. In particular, loads() and load() are used to read JSON from strings and files, respectively.

Python `

import json

with open('data.json', 'r') as file: data = json.load(file)

print(json.dumps(data, indent=4))

`

**Output:

{
"emp_details": [

{
"emp_name": "Shubham",
"email": "ksingh.shubh@gmail.com",
"job_profile": "intern"

},
{
"emp_name": "Gaurav",
"email": "gaurav.singh@gmail.com",
"job_profile": "developer"

},
{
"emp_name": "Nikhil",
"email": "nikhil@geeksforgeeks.org",
"job_profile": "Full Time"
}
]
}

Error Handling While Reading JSON

We can handle common errors while reading JSON using try-except blocks to make our code more reliable, maintainable and production-ready.

1. FileNotFoundError

This error occurs when Python cannot locate the JSON file you are trying to read. Handling it prevents your program from crashing when the file is missing.

import json

try: with open('data.json', 'r') as file: data = json.load(file) print("File data =", data)

except FileNotFoundError: print("Error: The file 'data.json' was not found.")

`

**Output if file missing:

Error: The file 'data.json' was not found.

2. JSONDecodeError

This error occurs when the JSON data is malformed or not properly formatted. Handling it ensures your program doesn’t crash due to invalid JSON.

Python `

import json

try: with open('data.json', 'r') as file: data = json.load(file) print("File data =", data)

except json.JSONDecodeError: print("Error: Failed to decode JSON from the file.")

`

**Output if JSON is malformed:

Error: Failed to decode JSON from the file.

Deserialize a JSON String to an Object in Python

JSON Data Type Python Object Type
Object dict
Array list
String str
Number (integer) int
Number (floating-point) float
True True (bool)
False False (bool)
null None