With AWS DynamoDB calls, we sometime get complex of item which precise the type of every element in the item.
It can be useful but it’s a mess to extract the data.
{
"a": {
"S": "AAAA"
},
"myList": {
"L": [
{
"S": "T1"
},
{
"S": "T2"
},
{
"S": "TH"
}
]
},
"checkList": {
"L": [
{
"M": {
"id": {
"S": "telesurveyor"
},
"check": {
"BOOL": true
}
}
}
]
},
"createdAt": {
"N": "1672842152365"
},
}
We need to transform it to this:
{
"a": "AAAA",
"myList": ["T1","T2","TH"],
"checkList": [
{
"id": "telesurveyor",
"check": true
}
],
"createdAt": 1672842152365,
}
Is there an AWS boto3 way to do it ?
If yes what is it ? If no, how to do it manually ?
2
Answers
Use the high-level Table interface instead of the low-level Client interface. The former auto-marshals values between native DynamoDB attributes (e.g.
"count" : { "N": "23" }
) and native Python (e.g."count": 23
)For example:
You can achieve this in two ways:
1. Resource Table Client
This is a higher level client which abstracts DynamoDB JSON and allows you to use native JSON objects for both writing and reading to/from DynamoDB.
Client instantiation is similar to before except you state the
Table
resource and provide it with a table name.2. TypeSerializer
The second option is useful if you have already used the low level client but want to unmarshall data for specific purposes. Its uses boto3
TypeSerializer
: