1

I pass a $input.json('$') body from the apigateway which is x-application/urlencoded to a Lambda function and insert into a Dynamodb. The event.body is like this

event.body

How can I unescape this string and get the JSON object? I've tried JSON.parse() and something like .replace slashes but it didn't work.

1
  • use Json.parse () twice. Commented Mar 25, 2019 at 8:46

2 Answers 2

3

You can use the $util.parseJson()

EDIT: Example:

#set ($bodyObj = $util.parseJson($input.body))

then use it :

{ 
    "TableName": "Mytable",
    "Item": {
    "commentId": {
            "S": "$context.requestId"
            },
        "pageId": {
            "S": "bodyObj.pageId"
            },
        "userName": {
            "S": "bodyObj.userName)"
        },
        "message": {
            "S": bodyObj.message"
        }
    }
}

example modified from https://aws.amazon.com/blogs/compute/amazon-api-gateway-mapping-improvements/

also might be worth checking the https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but,how do you write in the template?I am tring "body": $util.parseJson($input.body),but get the could not parse json response.
Edited the answer with extra examples
0

I use the json.loads and json.dump functions. And I get the same kind of string in DynamoDB whenever I store something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.