How can I remove attributes/elements from JSON response payload

I am using api Baas as my backend. On a get request, uuid, createdAt, modifiedAt, metadata attributes are part of each entity that is returned. I do not want to pass this back to the client making the request. How do you do this?

I tried delete operator using javascript callout. Didnt work.

Tried with Assign message policy using <Remove> tag. But entire payload is getting deleted.

Any options to do this?

Solved Solved
1 2 16.7K
1 ACCEPTED SOLUTION

yes, you can use the delete operator in a Javascript callout. But this changes only the object, or hash, in memory. It does not directly affect the response payload. You must write out the response payload again with the JSON version of the modified hash.

Example:

var b1 = JSON.parse(response.content),
    propertiesToRemove = ['status', 'refresh_token_status',
                         'token_type', 'organization_name',
                         'api_product_list', 'application_name'];


    propertiesToRemove.forEach(function(item){
      delete b1[item];
    });


// if there is no refresh token, don't keep properties related to it:
if( ! b1.refresh_token ) {
  delete b1.refresh_token_expires_in;
  delete b1.refresh_count;
}
context.setVariable('response.content', JSON.stringify(b1, null, 2));

View solution in original post

2 REPLIES 2

yes, you can use the delete operator in a Javascript callout. But this changes only the object, or hash, in memory. It does not directly affect the response payload. You must write out the response payload again with the JSON version of the modified hash.

Example:

var b1 = JSON.parse(response.content),
    propertiesToRemove = ['status', 'refresh_token_status',
                         'token_type', 'organization_name',
                         'api_product_list', 'application_name'];


    propertiesToRemove.forEach(function(item){
      delete b1[item];
    });


// if there is no refresh token, don't keep properties related to it:
if( ! b1.refresh_token ) {
  delete b1.refresh_token_expires_in;
  delete b1.refresh_count;
}
context.setVariable('response.content', JSON.stringify(b1, null, 2));

Thanks a ton this worked. I was trying to set response.content = b1;