How to remove few fields from JSON array response?

I have resolved this issue placing a Javascript Policy between Extract variable policy and Assign Message policy.

Inside Javascript policy, I’m taking the whole payload in a variable, converting it to an array, mapping required fields to another array, and then converting the second array to JSON object.

Extract Variable Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-FullData">
    <DisplayName>EV-FullData</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="resp">
            <JSONPath>$</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>apigee</VariablePrefix>
</ExtractVariables>

Javascript Policy code:

var res = context.getVariable("apigee.resp");
var result = [];
var result = JSON.parse(res);
var newArr = result.map(item => { return {
  id: item.id,
  name: item.name
}});
var output = JSON.stringify(newArr);
context.setVariable("apigee.resp",output);

Assign Message Policy code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-FinalResponse">
    <DisplayName>AM-FinalResponse</DisplayName>
    <Properties/>
    <Set>
        <Payload>
            {apigee.resp}
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
2 Likes