Object of type 'Decimal' is not JSON serializable AWS Lambda - DynamoDB
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with AWS Lambda in conjunction with DynamoDB, a common problem developers encounter is related to the serialization of data types, specifically when dealing with the Decimal type. DynamoDB often uses Decimal to represent numbers as it offers arbitrary precision, which is beneficial for accurately handling large numbers or high-precision monetary values. However, this data type isn't natively supported by JSON serialization, leading to errors and headaches during development.
Understanding the Error
When you attempt to serialize a Python object containing a Decimal type to JSON, you might encounter the error:
This occurs because the default Python json module does not understand how to convert Decimal to a format that can be represented in JSON, which typically only supports basic numeric types like int and float.
Example Scenario
Consider the following excerpt from a Python Lambda function interacting with DynamoDB:
In this scenario, the Decimal type for the balance field is not JSON serializable, causing the Lambda to fail when attempting to return the item as a response.
Solutions to the Problem
Custom Encoder
One approach to resolve this issue is to create a custom JSON encoder. Here's how you can implement it:
This custom encoder checks if the object is of type Decimal and converts it to a float before serialization.
Using Decimal to Float Conversion
Another straightforward method is converting Decimal to float directly before serialization:
Key Points Summary
| Aspect | Description |
| Error Source | Occurs when attempting to serialize Decimal type using Python's default json module. |
| Typical Scenario | Often seen in AWS Lambda functions when interacting with DynamoDB, which uses Decimal for numeric attributes. |
| Common Solution | Implement a custom JSONEncoder to handle Decimal types. |
| Alternative Solution | Convert Decimal to float before serialization. |
| Consideration in Solution Choice | Converting to float can introduce precision issues with high precision decimals. |
Additional Tips
- Precision Consideration: When converting
Decimaltofloat, be cautious as this may lead to loss of precision if theDecimalvalue has a high number of decimal places. - Use Libraries: Consider using libraries like
simplejsonwhich provide enhanced support for more complex data types. - DynamoDB Client Configuration: Boto3 automatically converts
Decimalto Pythonfloatif you specify the correct parameter when callingTableresource methods, simplifying your task further.
Understanding and mitigating serialization issues in your AWS Lambda functions ensures robustness and reliability when interacting with AWS services. By considering these solutions and adopting best practices, you can efficiently handle serialization of Decimal types and improve the efficiency of your Lambda functions.

