Vinit Kumar
Vinit Kumar

Reputation: 75

Handling Nested Integration response from aws apigateway

I have a JSON response from AWS Lambda going to AWS API-GATEWAY as follows :-

[
   {
       "key1" : "fruit",
       "key2" : "citrus",
       "key3" : {
                  "key31" : "lemon",
                  "key32" : "orange",
                  "key33" : "lime"
                }
   },

   {
       "key1" : "vegetable",
       "key2" : "green",
       "key3" : {
                  "key31" : "spinach",
                  "key32" : "lettuce",
                  "key33" : "cabbage"
                }
   }

]

Before sending to the client application from API Gateway, I want to modify the keys in the response as below:

[
   {
       "category" : "fruit",
       "subCategory" : "citrus",
       "examples" : {
                  "eg1" : "lemon",
                  "eg2" : "orange",
                  "eg3" : "lime"
                }
   },

   {
       "category" : "vegetable",
       "subCategory" : "green",
       "examples" : {
                  "eg1" : "spinach",
                  "eg2" : "lettuce",
                  "eg33" : "cabbage"
                }
   }

]

In AWS ApiGateway we have Mapping Templates to transform the response coming from Lambda and going out of API Gateway using the Apache Velocity. I am using application/json format to create the mapping template.

Below is the code I am written for the transformation --

#set($inputRoot = $input.path('$'))
[
    #foreach($elem in $inputRoot)
    {
      "category": "$elem.key1",
      "subCategory": "$elem.key2",
      "examples" : #set($example in $elem.key3) 
                 {
                  "eg1" : "$example.key31",
                  "eg2" : "$example.key32",
                  "eg3" : "$example.key33"
                 }#end
    }#if($foreach.hasNext),#end
#end
]

The response which I am receiving from the api gateway after hitting it as below ---

{
  "message": "Internal server error"
}

I am still new with the API Gateways so if anyone could help, it would be really great.

Upvotes: 1

Views: 983

Answers (0)

Related Questions