Error Handling with Python

Error Handling

When working with JSON in Python, various errors can occur during serialization (converting Python objects to JSON) or deserialization (converting JSON to Python objects). Understanding and handling these errors is crucial for robust and reliable code. Below are common errors and strategies for managing them.

Serialization Errors

  • TypeError during Serialization

Problem: A TypeError occurs if you attempt to serialize an object type that is not supported by the json module. Common unsupported types include custom objects, sets, and dates.

Example of TypeError: 

import json
from datetime import datetime
data = {
    "name": "Alice",
    "birthdate": datetime(1994, 7, 14)  # datetime object not serializable by default
}
try:
    json_string = json.dumps(data)
except TypeError as e:
    print(f"Serialization error: {e}")
"""
Output:
Serialization error: Object of type datetime is not JSON serializable
"""

Solution: Use the default parameter to provide a custom serialization function that converts non-serializable objects to a compatible format.

Example Solution: 

import json
from datetime import datetime
def custom_encoder(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError("Type not serializable")
data = {
    "name": "Alice",
    "birthdate": datetime(1994, 7, 14)
}
try:
    json_string = json.dumps(data, default=custom_encoder)
except TypeError as e:
    print(f"Serialization error: {e}")
"""
Output:
{"name": "Alice", "birthdate": "1994-07-14T00:00:00"}
"""

Deserialization Errors

  • JSONDecodeError during Deserialization

Problem: A JSONDecodeError occurs if the JSON being deserialized is malformed or invalid. This can include syntax errors such as extra commas, missing quotes, or improperly closed brackets.

Example of Invalid JSON: 

import json
json_string = '{"name": "Alice", "age": 30,}'
try:
    data = json.loads(json_string)
except json.JSONDecodeError as e:
    print(f"Deserialization error: {e}")
"""
Output:
Deserialization error: Expecting value: line 1 column 29 (char 28)
"""

Solution: Ensure that the JSON is correctly formed before deserialization. You can use online JSON validators or validation functions in your code.

  • ValueError during Conversion

Problem: A ValueError can occur if you attempt to deserialize JSON into a structure that does not match the expected format, such as incorrect values for specific data types.

Example of Incorrect Value: 

import json
json_string = '{"name": "Alice", "age": "thirty"}'  # "age" should be a number
try:
    data = json.loads(json_string)
except ValueError as e:
    print(f"Value error: {e}")
"""
Output:
Value error: invalid literal for int() with base 10: 'thirty'
"""

Solution: Ensure that JSON data is in the expected format before using it. You can perform additional validation on the data after deserialization.

Handling Errors with try-except

Using try-except blocks is an effective way to catch and handle errors during JSON manipulation. Here’s a general example of error handling with try-except.

General Example: 

import json
# Example of malformed JSON string
json_string = '{"name": "Alice", "age": 30, "city": "Paris"'
try:
    data = json.loads(json_string)
except json.JSONDecodeError as e:
    print(f"Deserialization error: {e}")
except TypeError as e:
    print(f"Type error: {e}")
except ValueError as e:
    print(f"Value error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")
"""
Output:
plaintext
Copier le code
Deserialization error: Expecting ',' delimiter: line 1 column 35 (char 34)
"""

Explanation:

  • json.JSONDecodeError: Captures errors related to incorrect JSON syntax.
  • TypeError: Captures errors related to non-serializable or incompatible types.
  • ValueError: Captures errors related to incorrect values or invalid conversions.
  • Exception: Captures any other unexpected exceptions.

Validating JSON Data

To avoid errors during serialization or deserialization, you can validate JSON data before processing it.

JSON Data Validation:

  • Check Structure: Ensure that the JSON data conforms to the expected structure, such as using JSON schemas or validation libraries.
  • Use Validation Tools: Use online tools or Python libraries like jsonschema to validate JSON data against schemas.

Example with jsonschema: 

import json
from jsonschema import validate, ValidationError
# Example JSON schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "city": {"type": "string"}
    },
    "required": ["name", "age", "city"]
}
# Example JSON data
data = {
   "name": "Alice",
    "age": 30,
    "city": "Paris"
}
# Convert to JSON and validate
json_string = json.dumps(data)
try:
    json_data = json.loads(json_string)
    validate(instance=json_data, schema=schema)
    print("Data is valid.")
except json.JSONDecodeError as e:
    print(f"Deserialization error: {e}")
except ValidationError as e:
    print(f"Validation error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")
"""
Output:
Data is valid.
"""

Explanation:

  • validate(): Validates JSON data against a provided schema.
  • Ensures that the JSON data conforms to the expected structure and types.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *