Hexie
Hexie

Reputation: 4221

API Gateway integration response model template - remove a property

I've contacted AWS support and was told that this is currently not possible, however, I struggle to believe that and would like a second opinion.

My situation is this:

I currently have an API and we use model templating on both the requests and responses... Something that looks like this (within the Integration Request):

#set($input = $input.body)
#set( $newbody = $input.replace("CostRecoveryGUID", 
"CostRecoveryId").replace("InvoiceItemGUID", "InvoiceItemId"))
$newbody

This returns the model as expected, however, it first replaces a few property names - this works perfectly fine.

I would like to receive the body object (within the response) and remove an entire property from the response. I know this might sound like a weird case and it is, however, this is needed.

So, assuming the same code - I tried something like this:

#set($input = $input.body)
#set( $newbody = $input.replace("CostRecoveryGUID", 
"CostRecoveryId").replace("InvoiceItemGUID", 
"InvoiceItemId").remove("PropertyName"))
$newbody

Which didn't work unfortunately.

I have also tried

#set( $newbody = $newbody.delete("PropertyName") 

and a few variations.

Is there anyone else that has a possible solution?

Thanks

Upvotes: 2

Views: 3279

Answers (2)

Hexie
Hexie

Reputation: 4221

Although Ka Hou leong has already answered this, I found another way to achieve the same thing without having to use Regex, if anyone else is ever interested.

To remove a property completely, we must treat it as an object. For example, you can use the $input.path(x) method[1] to obtain an object representation of the JSON. Once we have an object, we can perform object-related methods like "remove" on the object. Snippet below:

##get an object representation of the JSON string
#set($input = $input.path('$'))
##This removes property obj1 from the input json
#set($val = $input.remove("obj1"))
##reconstruct the JSON from the object if required
{
    #foreach($key in $input.keySet())
        #set($x = $input.get($key))
       "$key": "$x"
    #end
}

Testing:

When I gave the following input body ,

{
   "obj1": "1st object",
   "obj2": "2nd object"
}

I was able to obtain the following Endpoint request body after transformations:

{
    "obj2": "2nd object"
}

That is, obj1 was removed.

Note: This will return back a string result and currently (confirmed with AWS Support) there is no native function to convert the string back into JSON, hence, you would need to build it back up manually or by using the above for each snippet. I am still trying to find out another way around this.

Upvotes: 1

Ka Hou Ieong
Ka Hou Ieong

Reputation: 6535

Unfortunately, API Gateway doesn't support removing Property from the body in JSON format natively, but you can achieve that by using .replaceAll("regex", "replacement"), like string manipulation operation.

If you want to remove the property that is named test2 from your JSON body, you can define the template like this,

Template:

#set($propertyRegex = ',?\s*"?test2"? *: *"?\w*"?')
$input.body.replaceAll($propertyRegex, "")

Before Transformation:

{
    "test1" : "value1",
    "test2" : "value2",
    "test3" : "value3"
}

After Transformation:

{
    "test1" : "value1",
    "test3" : "value3"
}

I hope it helps

Upvotes: 2

Related Questions