简体   繁体   中英

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 -

  • Firstly, remove the ScannedCount value. (keep the Count one)
  • Then remove all the "S": "<value>" : value should appear directly, like "StoreID": "STR100"

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: :)

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
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM