PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] How to correctly format JSON when sending between two Lambdas?

 July 25, 2022     amazon-web-services, aws-lambda, json, python     No comments   

Issue

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")

Solution

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.



Answered By - JoseM
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing