kartik
kartik

Reputation: 602

How should I modify the response which is returned to me by AWS DynamoDB Query through AWS API Gateway?

I have built a Query API for AWS DynamoDB through the AWS API Gateway. Here's a sample response I get after a successful query :

{
   "Count":2,
   "Items":[
      {
         "StoreID":{
            "S":"STR100"
         },
         "OrderID":{
            "S":"1019"
         },
         "Date":{
            "S":"22nd May"
         }
      },
      {
         "StoreID":{
            "S":"STR100"
         },
         "OrderID":{
            "S":"1020"
         },
         "Date":{
            "S":"22nd May"
         }
      }
   ],
   "ScannedCount":2
}

What I want to do is, when the response reaches the API Gateway (before it reaches the user) I want to modify it. I would like to do the following changes to the coming response -

So, the modified response should look like :

{
   "Count":2,
   "Items":[
      {
         "StoreID":"STR100",
         "OrderID":"1019",
         "Date":"22nd May"
      },
      {
         "StoreID":"STR100",
         "OrderID":"1020",
         "Date":"22nd May"
      }
   ]
}

I hope you understand my problem - Any help is appreciated!
Thanks! :)

Upvotes: 1

Views: 392

Answers (1)

MaiKaY
MaiKaY

Reputation: 4482

Following the blog article Using Amazon API Gateway as a proxy for DynamoDB

Navigate to Integration Response and expand the 200 response code by choosing the arrow on the left. In the 200 response, expand the Mapping Templates section. In Content-Type choose application/json then choose the pencil icon next to Output Passthrough.

And for your example the template should look like this:

#set($inputRoot = $input.path('$'))
{
    "Count": "$inputRoot.Count",
    "Items": [
        #foreach($elem in $inputRoot.Items) {
            "StoreID": "$elem.StoreID.S",
            "OrderID": "$elem.OrderID.S",
            "Date": "$elem.Date.S"
        }#if($foreach.hasNext),#end
        #end
    ]
}

Upvotes: 2

Related Questions