1

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.

Vinit Kumar
  • 55
  • 10
  • One quick recommendation for your API model: instead of your api returning "eg1", "eg2", "eg3". Why not return a list of examples "examples" : [ "spinach", "lettuce", "cabbage"]. This seems like it more easily allows a varying number of examples for both the consumer and your velocity. – TheClassic Mar 09 '20 at 07:12
  • Check your API Gateway logs in CloudWatch. I'm not sure why you're getting an internal server error, but I'm sure the logs will tell you. If you can't find logs, check your API Gateway configuration to make sure they're enabled. – TheClassic Mar 09 '20 at 07:14
  • 1
    @TheClassic the model is predefined. I cannot restructure it. I checked the cloudwatch logs and the issue was in the synatx. In the second set method, there should be a `=` rather `in`. Thank you very much for the suggestion. – Vinit Kumar Mar 09 '20 at 20:21

0 Answers0