0

I have a lambda func that gets values from my DynamoDB table. The String values are printed to my cloud watch logs but look like this {'S': 'Random String here'} . How can I just get the string by itself. I understand that the S represents the attribute value for string https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html . Lambda Code:

import json
import boto3
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):
    e = event['Records'][0]
    message = e['dynamodb']['NewImage']['latestMessage']
    print(message) # This prints {'S': 'Random message here'}

How can I get the string without the {'S': ' '}

2
  • 1
    What about: print(message['S'])? Commented Feb 3, 2021 at 7:03
  • @Marcin This solution worked and is simple! Commented Feb 3, 2021 at 17:15

1 Answer 1

1

You can use the Boto DynamoDB Serializer/Deserializer to convert between DynamoDB and Python Objects.

import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
deserializer = boto3.dynamodb.types.TypeDeserializer()

def lambda_handler(event, context):
    e = event['Records'][0]
    message = e['dynamodb']['NewImage']
    deserialized = {k: deserializer.deserialize(v) for k,v in message.items()}
    print(deserialized) # This will print {'latestMessage': 'Random message here'}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.