The Complete Guide to JSON Handling and API Communication in Python | Validation and Security Measures Explained

1. What is JSON? (Basic Knowledge)

Overview of JSON

JSON (JavaScript Object Notation) is a data format commonly used for communication between clients and servers. It is lightweight, easy to read, and has a simple structure, making it widely used in web and mobile applications. JSON is a text-based format, making it highly versatile and easily handled by any programming language.

Basic Structure of JSON

JSON represents data using key-value pairs. For example, the following JSON structure:

{
  "name": "Sato",
  "age": 30,
  "hobbies": ["Reading", "Movies"]
}

In this example, "name" is a string, "age" is a number, and "hobbies" is an array. The simple and readable structure makes data exchange highly efficient.

Advantages of JSON

  1. Lightweight and Efficient: Since it is a text-based format with minimal data size, it reduces network load.
  2. Highly Compatible: Supported by almost all programming languages, making it usable across multiple platforms.
  3. Easy to Parse: Simple to read and write, making it a powerful tool, especially for API communication.

2. Handling JSON in Python (Basics)

The json Module in Python

Python provides the json module, which allows easy reading and writing of JSON data. For example, to convert JSON data into a Python dictionary, use the json.loads() function:

import json

json_data = '{"name": "Sato", "age": 30}'
python_obj = json.loads(json_data)

print(python_obj)  # {'name': 'Sato', 'age': 30}

Conversely, to convert a Python object into JSON format, use json.dumps():

python_obj = {"name": "Sato", "age": 30}
json_data = json.dumps(python_obj, ensure_ascii=False)

print(json_data)  # {"name": "Sato", "age": 30}

Reading and Writing JSON Files

You can also read JSON data from a file or write JSON data to a file.

# Reading from a file
with open('data.json', 'r') as f:
    data = json.load(f)

# Writing to a file
with open('data.json', 'w') as f:
    json.dump(python_obj, f, ensure_ascii=False)
侍エンジニア塾

3. JSON Communication in Python (Practical Use)

API Communication Using the requests Module

The requests module allows easy sending and receiving of JSON data via APIs. Below is an example of sending a POST request with JSON data and receiving a response.

Sending JSON Data via a POST Request

import requests

url = 'https://example.com/api'
data = {'name': 'Sato', 'age': 30}

response = requests.post(url, json=data)
json_response = response.json()

print(json_response)

Receiving JSON Data via a GET Request

You can also retrieve JSON data from an API using a GET request.

response = requests.get('https://example.com/api/user/1')
data = response.json()

print(data)

 


4. Error Handling and Best Practices

Error Handling in API Communication

Proper error handling is crucial when errors occur during API communication. The following example catches exceptions such as network errors and timeouts:

try:
    response = requests.post(url, json=data)
    response.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print("HTTP Error:", errh)
except requests.exceptions.ConnectionError as errc:
    print("Connection Error:", errc)
except requests.exceptions.Timeout as errt:
    print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
    print("Oops! Something went wrong:", err)

Retry Mechanism

When dealing with unstable network connections, implementing a retry mechanism is important. The Retry class in the requests module makes this easy:

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

response = session.get(url)

5. JSON Validation

Validation Using jsonschema

To ensure that the JSON data received from an API is in the expected format, you can use the jsonschema library for validation.

from jsonschema import validate, ValidationError

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "number"}
    },
    "required": ["name", "age"]
}

json_data = {"name": "Sato", "age": 30}

try:
    validate(instance=json_data, schema=schema)
    print("JSON is valid")
except ValidationError as e:
    print("Validation Error:", e)

It supports complex schemas and nested objects, ensuring that the data received from an API meets the required specifications.

6. Best Practices for Security

Managing API Keys

API keys should not be hardcoded into the source code. Instead, they should be stored in environment variables to enhance security.

import os

api_key = os.getenv('API_KEY')

Sanitizing Data

Before sending user input to a server, it is essential to sanitize the data properly to prevent security risks such as SQL injection and Cross-Site Scripting (XSS).

from html import escape

safe_data = escape(user_input)