how to remove few columns from json response

Suppose I am getting JSON response:

{"firstName":"John","lastName":"Doe","city":"San Jose","state":"CA"}

Now, can I get below modified version (remove one property):

{"firstName":"John","lastName":"Doe","city":"San Jose"}

I tried using Extract Variables and Assign Message policy in the response

Solved Solved
0 5 1,609
1 ACCEPTED SOLUTION

That is not the proper use of policies.

Extract Variable policy is used to extract the required variables,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-JSON">
    <DisplayName>EV-JSON</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="city">
            <JSONPath>$.city</JSONPath>
        </Variable>
        <Variable name="firstName">
            <JSONPath>$.firstName</JSONPath>
        </Variable>
        <Variable name="lastName">
            <JSONPath>$.lastName</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>Abhinaba</VariablePrefix>
</ExtractVariables>

As said, both policies should be used on Response side.

In Assign Message you need to use Set Payload,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Remove">
    <DisplayName>AM-Remove</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json">
	{
	  "firstName":"{Abhinaba.firstName}",
	  "lastName":"{Abhinaba.lastName}",
	  "city":"{Abhinaba.city}"
	}
	</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>


View solution in original post

5 REPLIES 5

Show the Extract Variables and Assign Message policy which you have used.

Below is the code for Extract Variable:

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

And here is the code for AM Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Remove">
    <DisplayName>AM-Remove</DisplayName>
    <Properties/>
    <Remove>
        <Headers>
            <Header name="firstName"/>
        </Headers>
        <Payload>firstName</Payload>
    </Remove>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

That is not the proper use of policies.

Extract Variable policy is used to extract the required variables,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-JSON">
    <DisplayName>EV-JSON</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="city">
            <JSONPath>$.city</JSONPath>
        </Variable>
        <Variable name="firstName">
            <JSONPath>$.firstName</JSONPath>
        </Variable>
        <Variable name="lastName">
            <JSONPath>$.lastName</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>Abhinaba</VariablePrefix>
</ExtractVariables>

As said, both policies should be used on Response side.

In Assign Message you need to use Set Payload,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Remove">
    <DisplayName>AM-Remove</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json">
	{
	  "firstName":"{Abhinaba.firstName}",
	  "lastName":"{Abhinaba.lastName}",
	  "city":"{Abhinaba.city}"
	}
	</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>


Why cant we use simple javascript and delete that object instead of having two policies?

We can do that, but if something can be done with policies then there is no need to use Javascript.

BTW Assign message policy alone is enough to both extract and set JSON payload using Message templates.