"altitude":"444.0","heading":"230.0","timestamplocal":"2018-12-07 18:37:24","fuelLevel":"359.04","location":"Srinagar - Kanyakumari Hwy,
I want to get only the following fields
vehicle_number,latitude,longitude,speed,distance,datetime
Please suggest me how can i go about it..
From 15 fields i want to filter only 5 fields
Hi Hima, you need to provide more info like,
will you get JSON in request/response?
when you say filter, what do you want to do with the filtered fields?
provide a valid JSON for better suggestions
The below example is based on the assumptions, JSON is received in Request and the filtered fields needs to be formed as a JSON, this can be done in multiple ways,
JSON coming in Request,
{
"altitude": "444.0",
"heading": "230.0",
"timestamplocal": "2018-12-07 18:37:24",
"fuelLevel": "359.04",
"location": "Srinagar - Kanyakumari Hwy"
}
Extracting location & fuelLevel fields using Extract Variable policy,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables">
<DisplayName>Extract Variables-1</DisplayName>
<Properties/>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<JSONPayload>
<Variable name="location">
<JSONPath>$.location</JSONPath>
</Variable>
<Variable name="fuelLevel">
<JSONPath>$.fuelLevel</JSONPath>
</Variable>
</JSONPayload>
<Source clearPayload="false">request</Source>
<VariablePrefix>hima</VariablePrefix>
</ExtractVariables>
Forming a JSON payload using Assign Message Policy,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
<DisplayName>Assign Message-1</DisplayName>
<Set>
<Payload contentType="application/json">
{
"extracted_location":"{hima.location}",
"extracted_fuelLevel":"{hima.fuelLevel}"
}
</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
The JSON will be available in **message.content** flow variable,
{"extracted_location":"Srinagar - Kanyakumari Hwy", "extracted_fuelLevel":"359.04"}
2 Likes
dknezic
December 11, 2018, 11:40am
4
Or if the fields you want are dynamic, you could use a javascript policy where you loop through the array of attributes to filter by to build a new object
newObject[attr] = oldObject[attr]
1 Like
How do i input the json in request or respone?
Siddharth, good answer.
Please note, you can also now skip the ExtractVariables policy.
<AssignMessage name='AM-Response'>
<AssignVariable>
<Name>json_path_location</Name>
<Value>$.location</Value>
</AssignVariable>
<AssignVariable>
<Name>json_path_fuelLevel</Name>
<Value>$.fuelLevel</Value>
</AssignVariable>
<Set>
<Payload contentType='application/json'>{
"extracted_location":"{jsonPath(json_path_location,request.content)}",
"extracted_fuelLevel":"{jsonPath(json_path_fuelLevel,request.content)}"
}</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew='false' transport='http' type='response'/>
</AssignMessage>
Thanks, Dino for the example. I guess it is high time I need to explore Message Templates and start using/suggesting them
I haven’t tried it, but I guess we can use this AssignMessage on both Request & Response flows right?
Other then request.content, if we are expecting a similar/different JSON fields through ServiceCallout or another AssignMessage before this AssignMessage policy, I guess we need to use calloutResponse.content and message.content respectively.
<Set>
<Payload contentType="application/json">
{
"serviceCallout":"{jsonPath(json_path_location,calloutResponse.content)}",
"assignMessage":"{jsonPath(json_path_fuelLevel,message.content)}",
"targetResponse":"{jsonPath(json_path_fuelLevel,response.content)}"
}
</Payload>
</Set>
If you are making a call to Apigee with Post method and JSON body, then this is a request JSON to APigee.
If the TargetEndpoint in Apigee is giving a JSON response, it is considered as JSON in response phase.
Yes, you can use this AssignMessage on both Request & Response flows.
and yes, with the jsonPath() function, you can reference any context variable that contains valid json.