drandom3
drandom3

Reputation: 199

JSON Post body not parsing as expected

I have a python function hosted on aws that takes an event and then does a bunch of things, but essentially the problem lies at parsing the incoming POST request.

My code looks like this:

import json

def lambda_handler(event, context):
    # TODO implement
    merchantid = json.dumps(event['body'])
    return {
        'statusCode': 200,
        'body': json.dumps(merchantid)
    }

To send the post request I am just using the aws API Gateway test that comes with the website, so it shouldn't be any weird encoding.

As I am passing in a JSON object, I expect the merchantid field to be storing a json object.

Instead, it returns this string:

"\"{\\n    \\\"merchantEmail\\\": \\\"[email protected]\\\",\\n    \\\"customerEmail\\\": \\\"[email protected]\\\",\\n    \\\"merchantWallet\\\": \\\"mWallet\\\",\\n    \\\"transactionTotal\\\": 1.00,\\n    \\\"transactionDenomination\\\": \\\"AUD\\\",\\n    \\\"customerCurrency\\\": \\\"EUR\\\",\\n    \\\"merchantAccess\\\" : {\\n        \\\"merchantKey\\\": \\\"key\\\",\\n        \\\"merchantSecret\\\": \\\"secret\\\"\\n    },\\n    \\\"customerAccess\\\" : {\\n        \\\"customerKey\\\": \\\"ckey\\\",\\n        \\\"customerSecret\\\": \\\"csecret\\\"\\n    }\\n}\""

I have never seen a string like this, and I can not seem to get this to return to JSON format.

Does anybody know how I can return this to the orignal JSON format that was submitted in the body?

I should mention that the lambda_handler works perfectly fine with the JSON using the test event, it was only once I started to try and trigger it with the API Gateway that I started having this problem.

edit: This is the JSON object I am passing as the body of the PUT request:

{
    "merchantEmail": "[email protected]",
    "customerEmail": "[email protected]",
    "merchantWallet": "mWallet",
    "transactionTotal": 1.00,
    "transactionDenomination": "AUD",
    "customerCurrency": "EUR",
    "merchantAccess" : {
        "merchantKey": "key",
        "merchantSecret": "secret"
    },
    "customerAccess" : {
        "customerKey": "ckey",
        "customerSecret": "csecret"
    }
}

edit: Before I attached the API Gateway I was handling it with

merchantid = event['merchantEmail']

but once I passed it in as the body of a PUT is would return a 502 internal server error

Upvotes: 0

Views: 1143

Answers (2)

Mrinal
Mrinal

Reputation: 125

First of all check this what is the datatype of the event params, Is it a string, or Is it dict.

if the datatype of body key value is JSON (dict)

merchantid = event.get('body').get('merchantEmail')

if the datatype of body key value is String (str)

merchantid = json.loads(event.get('body')).get('merchantEmail')

Upvotes: 1

balderman
balderman

Reputation: 23825

The event argument that you get into the Lambda function is a python dict.

Assuming you know the dict structure - all you need to do is the read the value you are looking for.

Example:

data = event['my_key']

Upvotes: 1

Related Questions