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 Answer
0
Accepted Answer

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}
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions