Registry / aws / dynamodb-json

dynamodb-json

JSON →
library1.4.2pypypi✓ verified 29d ago

dynamodb-json is a Python utility library designed to simplify the process of converting Python objects to and from the specific JSON format used by Amazon DynamoDB. It handles the marshalling and unmarshalling of various Python data types, including decimals and datetimes, into the DynamoDB-compatible JSON structure. The current version is 1.4.2 and it maintains an active release cadence, with recent updates focusing on compatibility and bug fixes.

pip install dynamodb-json
INSTALL
IMPORT
SIG · DYNAMODB-JSON
D
dynamodb-json
awspythonv1.4.2
Install
4.0s avg
Import
732ms
Disk
51MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.4.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.760s · 52.3MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 4.0s · import 0.704s · 53MB
51MB installed
● package 51MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

json_util
✓ from dynamodb_json import json_util as json
✗ import dynamodb_json
The primary functionality is exposed via the 'json_util' module, which is commonly aliased as 'json' for consistency with Python's standard json library.

This quickstart demonstrates how to use `dynamodb-json` to convert a standard Python dictionary containing various data types (including `datetime` and `Decimal`) into the DynamoDB-specific JSON format (`dumps`) and then convert a DynamoDB JSON string back into a Python dictionary (`loads`). This is crucial for interacting with the AWS DynamoDB API which expects this specific format.

import os from datetime import datetime from decimal import Decimal from dynamodb_json import json_util as json # Example Python object with various data types python_object = { "Id": 123, "Name": "Example Item", "IsActive": True, "Price": Decimal('99.99'), # DynamoDB expects Decimal for numbers "CreatedAt": datetime.now(), "Tags": ["aws", "python", "json"], "Details": { "Key": "Value", "Number": 456 } } # 1. Marshal Python object to DynamoDB JSON format dynamodb_json_format = json.dumps(python_object) print("DynamoDB JSON Format:") print(dynamodb_json_format) # Example DynamoDB JSON string (as if received from DynamoDB) # Note: datetime objects are converted to ISO 8601 strings during dumps # and then parsed back to datetime during loads if possible. # Decimals are preserved. db_json_string = '{"Id": {"N": "123"}, "Name": {"S": "Example Item"}, "IsActive": {"BOOL": true}, "Price": {"N": "99.99"}, "CreatedAt": {"S": "2026-04-10T18:54:00.000000"}, "Tags": {"L": [{"S": "aws"}, {"S": "python"}, {"S": "json"}]}, "Details": {"M": {"Key": {"S": "Value"}, "Number": {"N": "456"}}}}' # 2. Unmarshal DynamoDB JSON format back to Python object python_object_from_db = json.loads(db_json_string) print("\nPython Object from DynamoDB JSON:") print(python_object_from_db) print(f"Type of Price: {type(python_object_from_db['Price'])}") print(f"Type of CreatedAt: {type(python_object_from_db['CreatedAt'])}")
Debug
Known issues
breakingOlder versions of dynamodb-json (pre-1.3) might have compatibility issues with Python 3.4+ due to changes in `.next()` iterator syntax. Specifically, version 1.2 addressed Python 3.4+ compatibility and version 1.3 addressed Python 3.5+ compatibility by changing `.next()` to `next()`.
fix
Upgrade to dynamodb-json version 1.3 or higher (`pip install --upgrade dynamodb-json`) to ensure full compatibility with modern Python 3 environments.
affects: <1.3
gotchaDynamoDB does not natively support floating-point numbers and expects numerical values to be represented as `Decimal` types to avoid precision issues. While `dynamodb-json` handles this conversion during marshalling/unmarshalling, users must ensure their Python applications use `Decimal` for numbers that might otherwise be floats.
fix
Always use `decimal.Decimal` objects for numerical values in your Python data when interacting with DynamoDB via `dynamodb-json` to maintain precision and prevent DynamoDB `ValidationException` errors.
affects: All
gotchaDynamoDB has specific data type representations (e.g., `{'S': 'string'}`, `{'N': '123'}`). Manually constructing or parsing DynamoDB JSON without `dynamodb-json` can lead to `ValidationException` errors if the format or types are incorrect.
fix
Always use `dynamodb_json.json_util.dumps` and `dynamodb_json.json_util.loads` for converting between Python objects and DynamoDB's specific JSON format to ensure correct type marshalling and unmarshalling.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dynamodb-json'
The `dynamodb-json` library is not installed in the Python environment.
fix
Install the library using pip: `pip install dynamodb-json`
TypeError: Object of type 'Decimal' is not JSON serializable
DynamoDB often uses Decimal types for numbers, but Python's standard `json` module cannot serialize `Decimal` objects. This error occurs when attempting to use `json.dumps()` instead of `dynamodb_json.json_util.dumps()` on data containing Decimal objects.
fix
Use the `dumps` function from `dynamodb_json.json_util` to correctly serialize Decimal objects: `from dynamodb_json import json_util as json; dynamodb_json = json.dumps(my_data)`
TypeError: Object of type Binary is not JSON serializable
When retrieving items from DynamoDB using `boto3.resource`, binary data (DynamoDB 'B' type) is often represented as a `Binary` object, which the standard Python `json` module cannot serialize. This error occurs when using `json.dumps()` instead of `dynamodb_json.json_util.dumps()` on data containing Binary objects.
fix
Use the `dumps` function from `dynamodb_json.json_util` to correctly serialize Binary objects: `from dynamodb_json import json_util as json; dynamodb_json = json.dumps(my_data)`
AttributeError: module 'dynamodb_json' has no attribute 'dumps'
The `dumps` and `loads` functions are part of the `json_util` submodule within `dynamodb_json`, not directly under the `dynamodb_json` package itself. This error occurs if a developer tries to call `dynamodb_json.dumps()` after importing `import dynamodb_json` rather than `from dynamodb_json import json_util` or `from dynamodb_json import json_util as json`.
fix
Import the `json_util` submodule correctly: `from dynamodb_json import json_util as json` and then use `json.dumps()` or `json.loads()`.
Upgrade
Version history
1.4.2latest on PyPI · released May 18, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
16
Resources
dynamodb-json — pip install dynamodb-json · libregistry