How can I get statistics on the existence, or not, of a payload element in a response?

I want to be able to create an analytics report based on the existence of an element in the response payload, but not the value of the element.

For example, if my response includes multiple addresses (shippingAddress, billingAddress) I want to see a count of shippingAddress or a count by billingAddress.

0 3 104
3 REPLIES 3

@Kurt Kanaskie

How about creating a boolean variable based on the element is null or not & pushing same to analytics using statistics collector policy ?

Thanks, was thinking something along those lines.

I cannot extract directly as boolean using ExtractVariables:

"faultstring": "Unable to cast value {\"line\":\"240 market street\",\"city\":\"anytown\"} as BOOLEAN.",

Have to use string if matching an element structure (e.g. $.addresses.shippingAddress).

Also, cannot extract both in a single policy, if one is missing, even if continue on error is true, have to use 2 separate policies.

So maybe, rather than setting as boolean, I just need to populate a variable with values so I can see a count of shipping, billing or both.

Thought there may have been an easier way.

Former Community Member
Not applicable

Hi @Kurt Kanaskie probably the most efficient way to do that inside a javascript policy. You can get access to the response.content object & then use the right parser (JSON or XML) depending on the payload content-type. An example of checking for a property not existing could look something like:

Object = 
// replace 'property' with actual property that you are trying to check for
if (!("property" in object)) {
   // set some boolean variable to indicate property not set
   context.setVariable("someVariableNotFound", "true");
   // or do something else
}

All this could be done inside a single javascript policy.