I am trying to send JSON data from one parent lambda to multiple child lambdas. I am sending the data as JSON, it prints in the sending lambda with double quotes. However, the receiving lambda receives it only with single quotes:
event: [{'account': 'greg', 'caption': 'test test'}]
This is causing the error:
JSONDecodeError: Expecting property name enclosed in double quotes
I have tried using .encode()
and base64.b64encode()
but cannot get the correct format. What am I doing wrong? How do you send correctly formatted JSON between lambdas?
Sending Lambda:
response = client.invoke(
FunctionName = 'arn:aws:lambda:eu-west-1:xxxx:function:xxx',
InvocationType = 'RequestResponse',
Payload = json.dumps(sendData),
)
Receiving Lambda
def lambda_handler(event, context):
print("event: " + str(event))
handle_data(str(event))
return {
'statusCode': '200',
'body': 'Saved'
}
def handle_data(data):
data = json.loads(data)
print(data)
write_to_dynamo(data)
return print("Done")
2
Answers
The problem is that you aren’t sending JSON. A JSON document is a string. Use
json.dumps(e)
and send the result, this will encode it as valid JSON rather than using python’s built in output formatting.The problem is how you are sending the data to your
handle_data
method. Theevent
that you get in the lambda handler is already a list, you don’t need to do ajson.loads
on it. What you are currently doing is converting the python list to a string and then trying to decode it as json (which won’t work). Just send the data to your internal handler as is and don’t try to do a json.loads on it (the aws api already does that conversion for you automatically)Receiving Lambda
Relevant snippet from the documentation: