DynamoDB JSON Conversion Utility
dynamo-json is a Python utility library (currently v1.2.0) designed to facilitate seamless conversion between standard Python objects (including Decimal, datetime, and UUID) and the specialized JSON format used by Amazon DynamoDB. It provides `dumps` and `loads` functions that mimic the standard `json` library, handling the DynamoDB type descriptors (e.g., {'S': 'string'}, {'N': '123'}) automatically. The library has been in maintenance mode since its last update in 2020, with a focus on stable functionality.
Common errors
-
ImportError: cannot import name 'json_util' from 'dynamo_json' (or similar ImportError)
cause The `dynamo-json` package was either not installed, installed in a different Python environment, or there's a typo in the import statement.fixEnsure the package is installed: `pip install dynamo-json`. Verify the import statement: `from dynamo_json import json_util as json`. -
TypeError: Object of type Decimal is not JSON serializable (or datetime, UUID, etc.)
cause You are attempting to use Python's built-in `json` module (or another standard JSON library) to serialize objects containing types like `Decimal`, `datetime`, or `UUID` without a custom encoder. The `dynamo-json` library is specifically designed to handle these for DynamoDB compatibility.fixAlways use `dynamo_json.json_util.dumps()` and `dynamo_json.json_util.loads()` when working with data intended for or retrieved from DynamoDB, as these functions include the necessary custom encoders/decoders. -
Data appears incorrectly formatted or unreadable in the AWS DynamoDB Console's 'JSON' view.
cause The AWS DynamoDB Console's 'JSON' tab sometimes has limitations when displaying or converting complex DynamoDB JSON (with type descriptors) into a more human-readable 'plain JSON' format, especially for items with very large numbers or deeply nested structures. This is a display limitation of the console, not an issue with the data serialized by `dynamo-json`.fixTrust that `dynamo-json` correctly formats the data for DynamoDB. If you need to inspect the exact Python object, retrieve the item programmatically and use `dynamo_json.json_util.loads()` to deserialize it into a Python object.
Warnings
- gotcha There is a similarly named but distinct library called `dynamodb-json` (with 'db') developed by Alonreznik, which also provides DynamoDB JSON conversion functionalities and uses a very similar import pattern (`from dynamodb_json import json_util as json`). Ensure you install `dynamo-json` (`pip install dynamo-json`) if you intend to use this specific library by adilosa, to avoid unexpected behavior or missing features from the wrong package.
- gotcha DynamoDB has a strict item size limit of 400KB. While `dynamo-json` handles the format conversion, it does not bypass this underlying DynamoDB constraint. Attempting to serialize very large Python objects will result in DynamoDB errors when `put_item` is called.
- gotcha DynamoDB supports high-precision numbers (up to 38 digits) which are typically represented as strings in DynamoDB JSON (`{'N': '123.45'}`). This library correctly handles `Decimal` objects. If you process data outside of `dynamo-json`'s `dumps`/`loads` using standard JSON libraries, you might lose precision for large numbers or encounter `TypeError` for `Decimal` objects.
Install
-
pip install dynamo-json
Imports
- json_util
import json_util
from dynamo_json import json_util as json
Quickstart
import time
import uuid
from datetime import datetime
from decimal import Decimal
from dynamo_json import json_util as json
# Python object with various types, including those requiring special handling by DynamoDB
json_ = {
"MyString": "a",
"num": 4,
"MyBool": False,
"my_dict": {"my_date": datetime.now(), "my_uuid": uuid.uuid4()},
"MyList": [Decimal('1.23'), 2, 3],
"MyNull": None
}
# Convert Python object to DynamoDB JSON format
dynamo_json_data = json.dumps(json_)
print("DynamoDB JSON:\n", dynamo_json_data)
# Convert DynamoDB JSON format back to a Python object
python_object = json.loads(dynamo_json_data)
print("Python object:\n", python_object)