JSON (JavaScript Object Notation) is a popular format to store and exchange data. It’s used in web applications, APIs, and databases because of its lightweight and human-readable format. JSON structures data in key-value pairs, so it’s easy to work with across different platforms.
In Python, working with JSON files is easy because of Python’s built-in libraries, such as json which allows easy reading, parsing, and processing of JSON data. Whether you’re working with local files or fetching data from a web service, Python has tools to handle JSON data nicely.
This article will cover how to read JSON files in Python, load and parse JSON data, and working with its structures. You’ll also learn how to modify and write JSON data. Let’s get started!
Here’s a simple tutorial on how to read JSON files in Python:
Table of Contents
- Using
json.load()
to Read JSON from a File - Reading JSON from a String with
json.loads()
- Accessing Nested Data
- Modifying JSON Data
- Writing JSON Data to a File with
json.dump()
What are JSON Files?
A JSON file (JavaScript Object Notation file) is a text file used to store and exchange data. It stores data in a structured and readable way, using key value pairs, where each key is associated with a value. This makes it easy for humans and machines to read and write data. JSON is used in web applications, APIs, and configurations because it’s lightweight and easy to use.
Here’s an example of a simple JSON file:
1 | { |
In this example, the file contains information about a person named “Jane Smith” who is 28 and a developer. The languages
key holds an array of programming languages she knows.
You can read, write and save JSON files out of the box with Python. Let’s see how.
Loading JSON Data in Python
Loading JSON in Python is easy, thanks to the built-in json module. Whether the JSON is in a file or a string, Python has methods to load and parse it. In this section, we will cover two ways to load JSON: from a file and from a string.
Using json.load()
to Read JSON from a File
When working with a JSON file in Python, you can use json.load()
to load the data directly from the file. This method reads the file and parses it into a Python object (usually a dictionary).
Here’s an example of how to read a JSON file:
1 | import json |
In this code, We use open()
function to open the file in read mode (‘r’
) and then pass the file object to json.load()
to read and parse the JSON into a Python dictionary. Then, you can access the data using the keys.
For example, if your data.json
file contains the following:
1 | { |
The output will be:
1 | {'name': 'Alice', 'age': 25, 'isEmployed': True} |
Reading JSON from a String with json.loads()
If the JSON is a string, you can use json.loads()
to parse it. This is useful when working with JSON data retrieved from an API or other external source.
Here’s an example:
1 | import json |
In this example, json.loads()
takes the JSON string and turns it into a Python dictionary. You can then access the data as if it was from a file.
For example, the output will be:
1 | {'product': 'Laptop', 'price': 999, 'inStock': True} |
Next up we’ll cover the common operations you can do on JSON in Python.
Common Operations with JSON in Python
Once you have loaded JSON data into Python, you can do various things with it, like access nested data, modify it, and save the changes back to a file. Let’s go through these one by one with simple examples.
Accessing Nested Data
JSON data often has nested structures like dictionaries within dictionaries or lists within lists. Accessing this nested data in Python is easy using key-value access or list indexing.
Example:
1 | import json |
Here:
- We first load the JSON string using
json.loads()
. - We access nested data, such as the
city
inside theaddress
dictionary and the first skill from theskills
list.
Modifying JSON Data
You can easily modify JSON data in Python after loading it. Modifications can include updating values, adding new data, or removing existing keys.
Example:
1 | import json |
In this example:
- We modify the
age
value from 25 to 26. - We add a new key
skills
with an array of values. - The
json.dumps()
function is used to print the modified JSON data in a readable format with indentation.
Writing JSON Data to a File with json.dump()
After modifying JSON data, you might want to save it back to a file. The json.dump() function helps you write the data back to a file in JSON format.
Example:
1 | import json |
In this example:
- We modify and organize the data in a Python dictionary.
- The
json.dump()
method writes the data to a file namedmodified_data.json
. - The
indent=4
parameter makes the JSON file readable by adding indentation.
Learning these common operations (accessing nested data, modifying it, and saving it to a file) is very important for working with JSON files in Python. They allow you to manipulate and organize your data for many use cases.
Next up, we’ll cover error handling when reading JSON files so your programs don’t crash.
Error Handling When Reading JSON Files
When working with JSON data in Python, you need to handle errors that can happen when reading or parsing JSON files. Errors can occur due to many reasons such as invalid JSON syntax, incorrect file paths, or file encoding issues. Proper error handling will ensure your Python script runs smoothly and can recover from unexpected errors.
Let’s explore some common errors and how to handle them effectively in Python.
Handling File Not Found Error
If the specified JSON file does not exist or the file path is incorrect, Python raises a FileNotFoundError
. You can use a try-except
block to catch this error and display a user-friendly message.
Example:
1 | import json |
In this code:
- We attempt to read the
data.json
file. - If the file does not exist, the
FileNotFoundError
is caught, and a meaningful error message is printed.
Handling Invalid JSON Syntax
If the JSON file contains invalid syntax (e.g., missing commas, braces, or brackets), Python raises a json.JSONDecodeError
. You can handle this error using a try-except
block to prevent your program from crashing.
Example:
1 | import json |
Here:
- The
invalid_json
string is missing a closing bracket. - The
json.JSONDecodeError
is caught, and an error message specifying the issue is printed.
Handling Incorrect File Encoding
Sometimes, the JSON file might be saved with an encoding different from UTF-8, which can cause decoding errors when reading the file. Python’s UnicodeDecodeError
handles such cases, and you can specify the correct encoding while opening the file to avoid issues.
Example:
1 | import json |
In this code:
- We specify
encoding='utf-8'
when reading the file. - If there is a problem with the file encoding, a
UnicodeDecodeError
is caught and an appropriate error message is displayed.
General Exception Handling
You can also use a general except
block to catch any other unexpected errors that might occur when reading or working with JSON files.
Example:
1 | import json |
This code:
- Uses a general
Exception
to catch any errors that don’t fall into specific categories. - Prints the error message to help identify the problem.
Error handling is an essential part of working with JSON files, as it helps you manage issues like missing files, incorrect formats, and encoding problems. By catching these errors early, you can ensure that your Python scripts run more smoothly and are easier to debug.
Final Thoughts
Reading and working with JSON files in Python is a crucial skill for developers, especially when dealing with APIs, web applications, or data storage. Python’s built-in json module makes it easy to handle JSON data, whether you’re reading from a file, parsing a string, or modifying the data.
By learning how to load, manipulate, and write JSON data in Python, you can efficiently manage structured data in your projects. JSON’s flexibility and readability make it one of the most widely used formats today, and with Python’s tools, you can easily integrate it into any application.
With the techniques discussed in this blog, you should be well-equipped to handle JSON data in your Python projects, no matter the complexity of the data structures. For more tutorials like these, follow our blog. If you have any questions or feedback, our support team is here to help you.
Frequently Asked Questions (FAQs)
Q. What is the difference between json.load()
and json.loads()
in Python?
json.load()
is used to read and parse JSON data from a file, while json.loads()
is used to parse JSON data from a string. The s
in json.loads()
stands for “string”, so it’s useful when you have JSON data as a string, not in a file.
How do I convert a JSON string to a Python object?
You can convert a JSON string to a Python object using json.loads()
. This function parses the JSON string and returns a Python dictionary or list.
Example:
1 | import json |
Q. How do I write JSON data to a file in Python?
To write JSON data to a file, use json.dump()
. Open the file in write mode, then pass the Python object and file to json.dump()
to store the JSON data in the file.
Example:
1 | import json |
Q. How do I handle errors when working with JSON in Python?
Common errors when working with JSON include invalid JSON format or incorrect file paths. To handle these, you can use Python’s try-except
blocks to catch exceptions like json.JSONDecodeError
or FileNotFoundError
.
Example:
1 | import json |