1

in the event of lambda i am getting new and old images for any update in dynamo db .now in my new image have one property names as sourceDetails which can vary eg.(please note the string mentioned below is json

{'M': {'bucketName': {'S': 'postote-offers-bucket'}}}

or it be like for eg.

 {'M': {'streamName': {'S': 'someStream'},{'limit': {'N': 10}}}

How can i parse this result in best possible way

2
  • 1
    What do you mean by parse? What do you want to achieve? What have you tried so far? Commented Feb 12, 2019 at 12:57
  • i have a new and old image coming from dynamo db change in the associated lambda event .like newImage=json.loads(json.dumps(event['Records'][0]['dynamodb']['NewImage'])) .this gives me new image of event modify .now if i have to get bucket_name=newImage['InputSourceDetails']['M']['bucketName']['S'] i have to do something like this .Is there a better way to achieve the same . Commented Feb 13, 2019 at 6:22

1 Answer 1

3

You can use the jmespath library. It's already present in the python environment of Lambda, because it's a dependency of boto3.

path = 'Records[0].dynamodb.NewImage.InputSourceDetails.M.bucketName.S'
bucket_name = jmespath.search(path, event)

If there is no value under the path, the jmespath.search function will return None.

Btw you don't have to do the json.loads + json.dumps (as mentioned in the comment below the question), event is already a python dictionary.

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.