Formatting DynamoDB data to normal JSON in AWS Lambda

前端 未结 8 1390
無奈伤痛
無奈伤痛 2020-12-01 02:55

I\'m using AWS Lambda to scan data from a DynamoDB table. This is what I get in return:

{
  \"videos\": [
    {
      \"fil         


        
相关标签:
8条回答
  • 2020-12-01 03:34
    • Javascript: AWS SDK provides the unmarshall function

    • Python: use TypeDeserializer from boto3.dynamodb.types:

    from boto3.dynamodb.types import TypeDeserializer, TypeSerializer
    
    def from_dynamodb_to_json(item):
        d = TypeDeserializer()
        return {k: d.deserialize(value=v) for k, v in item.items()}
    
    ## Usage:
    from_dynamodb_to_json({
        "Day": {"S": "Monday"},
        "mylist": {"L": [{"S": "Cookies"}, {"S": "Coffee"}, {"N": "3.14159"}]}
    })
    # {'Day': 'Monday', 'mylist': ['Cookies', 'Coffee', Decimal('3.14159')]}
    
    0 讨论(0)
  • 2020-12-01 03:34

    If you are using python in the lambda you can utilise the dynamodb-json library.

    Install library

    pip install dynamodb-json
    

    and use the below snippet

    from dynamodb_json import json_util as util
    
    def marshall(regular_json):
        dynamodb_json = util.dumps(reular_json)
    
    def unmarshall(dynamodb_json):
        regular_json = util.loads(dynamodb_json)
    

    Reference https://pypi.org/project/dynamodb-json/

    0 讨论(0)
提交回复
热议问题