0

I am trying to process dynamoDB streams based on changes in a table.

The items from the stream have the data type present in the stream.

I am trying to find a library similar to dynamodb-data-types, which can unwrap the dynamoDB structure to json structure for python. My lambda is in python.

Eg: I am trying to convert

{
    "fruit": {
        "S": "Apple"
    },
    "count": {
        "N": "12"
    }
}

to

{
    fruit: 'Apple',
    count: 12
}

I tried to search in the wrb, but was unable to find and solution.

If there is no such library, what do you think is the best way to handle this myself.

Edit:

Currently I am achieving this using(I am not sure if this is the recommended way):

def cleanDynamoDBStream(record):
    newImage = record["dynamodb"]["NewImage"]
    for key, dynamoDBValue in newImage.items():
        for dataType, value in dynamoDBValue.items():
            newImage[key] = value
return newImage
tharun
  • 348
  • 6
  • 15

1 Answers1

2

from boto3 you can use the TypeDeserializer -- it's a bit of a weird api (unnecessarily object oriented) but it seems to do what you want:

>>> obj = {
...     "fruit": {
...         "S": "Apple"
...     },
...     "count": {
...         "N": "12"
...     }
... }
>>> from boto3.dynamodb.types import TypeDeserializer
>>> deserializer = TypeDeserializer()
>>> deserializer.deserialize({'M': obj})
{'fruit': 'Apple', 'count': Decimal('12')}

note that I had to wrap the object in {'M': ...} as the top level of your object is a map

anthony sottile
  • 61,815
  • 15
  • 148
  • 207