Complete Guide to Handling JSON Data in Python | From Reading to Writing

1. What is JSON?

Overview of JSON

JSON (JavaScript Object Notation) is a lightweight and highly efficient format for data exchange. It is primarily used for communication between web applications and servers. Although JSON is based on JavaScript, it is supported by almost all programming languages. It plays a crucial role in retrieving and sending data via APIs.

Structure of JSON

JSON consists of key-value pairs, with data enclosed in curly braces {}. Here’s a simple example:

{
    "name": "John",
    "age": 30,
    "isStudent": false,
    "courses": ["Math", "Physics", "Chemistry"]
}

This structure is highly flexible and can include lists and nested objects.

2. Basic Method for Reading JSON Files in Python

Introducing Python’s json Library

Python provides a built-in json library, making it easy to handle JSON files without any additional installations. This library includes fundamental functions for reading and writing JSON data.

How to Read a JSON File

We use the json.load() function to read data from a file. The following example demonstrates how to load JSON from a file and work with it as a dictionary.

import json

with open('data.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

print(data)
侍エンジニア塾

3. How to Read JSON Strings in Python

Using the json.loads() Function

To convert JSON data in string format into a Python dictionary, use the json.loads() function. This is commonly used when processing API responses containing JSON strings.

import json

json_string = '{"name": "Alice", "age": 25, "city": "Tokyo"}'
data = json.loads(json_string)

print(data)

This method allows you to easily transform JSON strings into dictionaries.

4. Important Considerations and Error Handling When Reading JSON Files

Common Errors and Solutions

One of the most common errors when reading a JSON file is JSONDecodeError. This occurs when the file is not in a valid JSON format. Proper error handling is crucial for identifying and addressing issues.

import json

try:
    with open('data.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
except json.JSONDecodeError as e:
    print(f"JSON Load Error: {e}")

Encoding Issues

When handling JSON files containing non-ASCII characters, such as Japanese text, specifying encoding='utf-8' helps prevent character corruption.

5. Efficiently Handling Large JSON Data in Python

Memory-Efficient Data Processing

Loading large JSON files entirely into memory can cause memory shortages. To avoid this, it is recommended to use streaming libraries such as ijson.

import ijson

with open('large_file.json', 'r', encoding='utf-8') as f:
    for item in ijson.items(f, 'item'):
        print(item)

Using ijson allows efficient processing of large-scale data.

6. Utilizing JSON Data After Converting It to a Python Dictionary

Basic Dictionary Operations

Converting JSON into a dictionary makes data manipulation much easier. For example, you can retrieve values for specific keys, update data, or add new key-value pairs.

# Example of dictionary operations
print(data['name'])  # Outputs 'Alice'
data['age'] = 26  # Updating a value
data['email'] = 'alice@example.com'  # Adding a new key-value pair
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

7. Writing JSON Data in Python

Writing Data to a JSON File

To write a Python dictionary as JSON data and save it to a file, use the json.dump() function. This allows you to store modified data easily.

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, indent=4, ensure_ascii=False)

The indent=4 option formats the output for readability, while ensure_ascii=False ensures that non-ASCII characters (such as Japanese text) are preserved correctly.