skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. The problem is how you are sending the data to your handle_data method. The event that you get in the lambda handler is already a list, you don’t need to do a json.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

    def lambda_handler(event, context):
         handle_data(event)
    
         return {
    
                'statusCode': '200',
    
                'body': 'Saved'
    
         }
    
    def handle_data(data):
         print(data)
    
         write_to_dynamo(data)
    
         return print("Done")
    

    Relevant snippet from the documentation:

    When Lambda invokes your function handler, the Lambda runtime passes two arguments to the function handler:

    The first argument is the event object. An event is a JSON-formatted document that contains data for a Lambda function to process. The Lambda runtime converts the event to an object and passes it to your function code. It is usually of the Python dict type. It can also be list, str, int, float, or the NoneType type.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search