1

The lambda function is receiving following message from SQS

"Records": [
    {
        "messageId": "a91c1641-fb7e-4ce5-88ad-2dd180aaada6",
        "receiptHandle": "AQEBngcKiVX9S0IjX6SfCm5VAtaDbSxznYLDMMoN2J5pq+uzVLdyzsaBVwXVjOE94dBnzAqkbI2II8RvfIfEeQCwp/8MxV5OLJXEQiBhpm2QwCXlUy+1ludjUbKRXT6pBux0eqjuVDk4I/vQ59m3D+ut2mafo2wgEZtUqT28P5DVgpeCgwFmuHcSRoc/vjcj/5rBDSJ6Mk4T+XTnnPMkZXOY0Nl9+XWzXJe3eX+DJC7vn6Vi6wEMk84nhbjn+CdL/TpHFtA6qCFE03XEJnRQeZCf69sqbpGq+ecIKI+bxb5DMDOTIin+ugb1oqpvfhySkB9nWodYOv+P6ANeLJSm4kBRvsSriDcP9uO/whyQGUMZC1v0Kx0CFVqtVmg2fIoj5R8k3f7QU9zpTKrJO5Bn/K6BwA==",
        "body": "[\n    {\n        \"year\": 2013,\n        \"title\": \"Rush\",\n        \"info\": {\n            \"directors\": [\"Ron Howard\"],\n            \"release_date\": \"2013-09-02T00:00:00Z\",\n            \"rating\": 8.3,\n            \"genres\": [\n                \"Action\",\n                \"Biography\",\n                \"Drama\",\n                \"Sport\"\n            ],\n            \"image_url\": \"http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg\",\n            \"plot\": \"A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.\",\n            \"rank\": 2,\n            \"running_time_secs\": 7380,\n            \"actors\": [\n                \"Daniel Bruhl\",\n                \"Chris Hemsworth\",\n                \"Olivia Wilde\"\n            ]\n        }\n    }\n]",
        "attributes": {
            "ApproximateReceiveCount": "1",
            "SentTimestamp": "1604156768953",
            "SenderId": "983336180496",
            "ApproximateFirstReceiveTimestamp": "1604156768958"
        },
        "messageAttributes": {},
        "md5OfBody": "3d249c6c5e1b6cffc0f7af7101f3ea40",
        "eventSource": "aws:sqs",
        "eventSourceARN": "arn:aws:sqs:us-east-1:983336180496:Parse-SQS-Msg",
        "awsRegion": "us-east-1"
    }
]

Now in the body section I want to extract details like "Year", "Title" and "Rating". How can I extract these details in different variables so that I can write logic on these accordingly? The details I want to extract are Year, Title, genres and rating:

"body": "[
    {
        "year": 2013,
        "title": "Rush",
        "info": {
            "directors": ["Ron Howard"],
            "release_date": "2013-09-02T00:00:00Z",
            "rating": 8.3,
            "genres": [
                "Action",
                "Biography",
                "Drama",
                "Sport"
            ],
            "image_url": "http://ia.media-imdb.com/images/M/MV5BMTQyMDE0MTY0OV5BMl5BanBnXkFtZTcwMjI2OTI0OQ@@._V1_SX400_.jpg",
            "plot": "A re-creation of the merciless 1970s rivalry between Formula One rivals James Hunt and Niki Lauda.",
            "rank": 2,
            "running_time_secs": 7380,
            "actors": [
                "Daniel Bruhl",
                "Chris Hemsworth",
                "Olivia Wilde"
            ]
        }
    }
]

The code snippet is below:

    from __future__ import print_function
    from pprint import pprint
    from botocore.vendored import requests
    import boto3
    import json
    
    dynamodb = boto3.resource('dynamodb')
    
    def lambda_handler(event, context):
        print("Received event: " + json.dumps(event, indent=2))
        for record in event['Records']:
           print ("test")
           year_e = record['body'][0]
           print(year_e)
           payload=record["body"]
           print(str(payload))

I tried several permutations and combinations from web but nothing helped. I am very new to Python so quick help is appreciated.

4
  • add the event (as text) to the question please. Please describe the problem - it is not clear. Commented Oct 31, 2020 at 11:14
  • That is not a valid JSON input. json.dumps() will serialize an obj as a JSON formatted stream out. And Lambda functions are small anonymous functions. Commented Oct 31, 2020 at 15:06
  • Provide a working sample. Commented Oct 31, 2020 at 15:10
  • I am getting "body" in payload. I need help in extracting year --> 2013 in one variable and title in other so that I can query db accordingly. Commented Oct 31, 2020 at 15:23

1 Answer 1

1

The issue is that the body field contains JSON, but it is in a text format. You will need to convert it to a Python object with:

body = json.loads(record['body'])

You can then reference the content as a Dictionary:

for film in body:
  print(film['title'], film['year'], film['info']['rating'])
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.