Using API Gateway Request Templates in Lambda Integration for parsing JSON results in empty string

0

Is it possible to pass parsed JSON to Lambda? Or does API Gateway support only passing strings to integrations where the string must be parsed as JSON?

The goal is to map a query string parameter myJson (as in GET /api/endpoint?myJson=%7B%5C%22x%5C%22%3A123%7D) to a parsed JSON object, such that it is accessible in the Lambda handler without the need for (using JavaScript) JSON.parse(event.queryStringParameters.myJson).

However, using the request template below:

{
    "myParsedJson": "$util.parseJson($input.params('myJson'))"
}

or

{
    "myParsedJson": "$util.parseJson($util.urlDecode($input.params('myJson')))"
}

gives an event in Lambda that is:

{
    myParsedJson: ''
}

What I expected is:

{
    myParsedJson: { x: 123 }
}

Any pointers?

1 個回答
0
已接受的答案

Hello,

You may just try this $util.urlDecode($input.params('myJson')) and provide the query string value GET /api/endpoint?myJson=%7B%22x%22%3A123%7D and it should work as you expect. you need not to worry about parsing as you are providing json string value of json with encode, hence by decoding you will get json string back. if you like to use stringify version containing "/" then you have to replace "/" with nothing to get json string. hope it helps. thanks

Code snippet:

"myParsedJson" : $util.urlDecode($input.params('myJson'))

output:

myParsedJson : {"x":123}
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南