Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

How to Read JSON Files in Python

8 min readSep 24, 2024

--

This article will cover how to read JSON files in Python, load and parse JSON data, and work with its structures. You’ll also learn how to modify and write JSON data.

Originally published at https://crawlbase.com.

Let’s get started!

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:

{
"name": "Jane Smith",
"age": 28,
"isDeveloper": true,
"languages": ["Python", "JavaScript", "SQL"]
}

In this example, the file contains information about a person named “Jane Smith” who is 28 and a developer. The language 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 methods for loading JSON: from a file and 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:

import json
# Open the JSON file and load its content
with open('data.json', 'r') as file:
data = json.load(file)
# Print the parsed data
print(data)

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:

{
"name": "Alice",
"age": 25,
"isEmployed": true
}
The output will be:
{'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:

import json
# JSON string
json_string = '{"product": "Laptop", "price": 999, "inStock": true}'
# Parse the string into a Python dictionary
data = json.loads(json_string)
# Print the parsed data
print(data)

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:

{'product': 'Laptop', 'price': 999, 'inStock': True}

Next, we’ll cover the common operations you can perform 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:

import json
# Sample JSON data
json_data = '''
{
"name": "Alice",
"age": 25,
"address": {
"city": "New York",
"zipcode": "10001"
},
"skills": ["Python", "Machine Learning"]
}
'''
# Parse the JSON data
data = json.loads(json_data)
# Accessing nested data
city = data['address']['city']
skill = data['skills'][0]
print(f"City: {city}")
print(f"First Skill: {skill}")

Here:

  • We first load the JSON string using json.loads().
  • We access nested data, such as the city inside the address dictionary and the first skill from the skills 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:

import json
# Sample JSON data
json_data = '''
{
"name": "Alice",
"age": 25,
"address": {
"city": "New York",
"zipcode": "10001"
}
}
'''
# Parse the JSON data
data = json.loads(json_data)
# Modify the age and add a new skill
data['age'] = 26
data['skills'] = ["Python", "Data Science"]
# Print the modified data
print(json.dumps(data, indent=4))

In this example:

  • We modify the age value from 25 to 26.
  • We add 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, save it back to a file. The json.dump() function helps you write the data back to a file in JSON format.

Example:

import json
# Sample data to write to file
data = {
"name": "Alice",
"age": 26,
"address": {
"city": "New York",
"zipcode": "10001"
},
"skills": ["Python", "Data Science"]
}
# Write JSON data to a file
with open('modified_data.json', 'w') as file:
json.dump(data, file, indent=4)
print("JSON data successfully written to file.")

In this example:

  • We modify and organize the data in a Python dictionary.
  • The json.dump() method writes the data to a file named modified_data.json.
  • The indent=4 parameter makes the JSON file readable by adding indentation.

Learning these everyday operations— accessing, modifying, and saving nested data 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, we’ll cover error handling when reading JSON files, ensuring 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 for various 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.

Get Crawlbase’s stories in your inbox

Join Medium for free to get updates from this writer.

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:

import json
try:
# Attempt to open a non-existent file
with open('data.json', 'r') as file:
data = json.load(file)
except FileNotFoundError:
print("Error: The file was not found. Please check the file path.")

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:

import json
invalid_json = '''
{
"name": "Alice",
"age": 25,
"address": {
"city": "New York"
} # Missing closing bracket
'''
try:
# Attempt to parse invalid JSON
data = json.loads(invalid_json)
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON format. {e}")

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:

import json
try:
# Open a file with a specific encoding
with open('data.json', 'r', encoding='utf-8') as file:
data = json.load(file)
except UnicodeDecodeError:
print("Error: Unable to decode the file. Please check the file's encoding.")

In this code:

  • We specify encoding=’utf-8' when reading the file.
  • If a problem occurs 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:

import json
try:
# Attempt to read JSON file
with open('data.json', 'r') as file:
data = json.load(file)
except Exception as e:
print(f"An unexpected error occurred: {e}")

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, particularly when interacting with APIs, web applications, or data storage systems. 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:

import json
json_string = '{"name": "John", "age": 30}'
data = json.loads(json_string)
print(data)

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:

import json
data = {"name": "John", "age": 30}
with open('data.json', 'w') as file:
json.dump(data, file)

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:

import json
try:
with open('data.json', 'r') as file:
data = json.load(file)
except FileNotFoundError:
print("File not found!")
except json.JSONDecodeError:
print("Invalid JSON format!")

Originally published at https://crawlbase.com.

Thank you for being a part of the community

Before you go:

--

--

Crawlbase

Written by Crawlbase

Premium data scraping tool, exploring the data universe, analytics, and everything in between, one byte at a time.

Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.