简体   繁体   中英

AWS SQS Triggering Python Lambda Function

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.

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM