I have a SQS queue which triggers a lambda function where as a message I pass the stringify json. I am trying to get the whole message body from the Records
, but throws an error,
[ERROR] KeyError: 'efsPathIn'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 20, in lambda_handler
key = bodyDict['efsPathIn']
I'm sending this stringify json as a message in SQS queue,
{"Records": [{"body": "{\"efsPathIn\": \"163939.jpeg\",\"imageWidth\": \"492\",\"imageHeight\": \"640\",\"bucketOut\":\"output-bucket\",\"keyOut\":\"163939.webp\"}"}]}
And the code is which extracts the values,
for item in event['Records']:
body = str(item['body'])
bodyDict = json.loads(body)
key = bodyDict['efsPathIn']
bucket_out = bodyDict['bucketOut']
width = bodyDict['imageWidth']
height = bodyDict['imageHeight']
key_out = bodyDict['keyOut']
I've tried with json.dumps(item['body'])
also which further loads the json, but still getting the same error.
When I test from AWS Lambda test console using the above mentioned json message, the function gets successfully executed, but I get this error while sending a message from a SQS queue.
json.dumps()
is for converting a Python object into a JSON string. You have a JSON string that you are need to convert into a Python object. You should be calling json.loads()
like so:
body = json.loads(item['body'])
After which you will have a Python object that you can do things like: body['efsPathIn']
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.