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.
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": "[email protected]",
"job_profile": "intern"},
{
"emp_name": "Gaurav",
"email": "[email protected]",
"job_profile": "developer"},
{
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
}
]
}
- In this example, we are reading data from the "data.json" file, and the output retains the same structured format as the original JSON content.
- json.dumps(obj, indent=4): converts the Python object back to a JSON string with 4-space indentation.
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.
- json.load(file): Reads JSON data from a file.
- open(filename, mode='r'): Opens a file safely for reading.
- with statement: Ensures the file is properly closed after reading.
- try-except: Captures the error and allows us handle it gracefully.
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.
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
- Deserialization is the process of converting JSON data into corresponding Python objects, such as dictionaries and lists.
- In Python, the json.load() and json.loads() methods are used for deserialization.
- json.load() reads and parses JSON data from a file object.
- json.loads() parses JSON data from a string.
- After deserialization, the top-level JSON object is typically converted into a Python dict or list, depending on the structure of the JSON data.
- The table below shows how JSON data types are mapped to their corresponding Python objects.
| 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 |