How do I extract values from a variable length JSON list using ExtractVariables Policy?

I need to extract all the elements of a list in a JSON package, e.g. here's the payload

{"alertInstanceId":"05985927-ab93-4933-9d62-51744d64af0e","alertName":"403 and 404 Not Found Test Webhook Test","org":"amer-demo13","description":"403 and 404 Not Found Test with Webhook","alertId":"6f8ff505-16db-11ea-ad50-42010a850821","alertTime":"2020-10-05T16:04:11Z","thresholdViolations":{"0 - Count":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0","1 - Count":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"},"thresholdViolationsFormatted":[{"metric":"count","proxy":"ALL","region":"us-east1","statusCode":"403","comparisonType":"above","thresholdValue":"5.0","triggerValue":"7","duration":"5 minutes","violation":"sustained above 5.0"},{"metric":"count","proxy":"ALL","region":"us-east1","statusCode":"404","comparisonType":"above","thresholdValue":"5.0","triggerValue":"7","duration":"5 minutes","violation":"sustained above 5.0"}],"playbook":"TEST2: Check out the 404 playbook for test"}

this ExtractVariable segment will give me the contents of "0 - Count",

<JSONPath>$.thresholdViolations.0 - Count</JSONPath>

but I'd like to extract every value in thresholdViolations into a separate variable, not knowing in advance how many items are in thresholdViolations, or even what are the names of each value in it.

This code will give me a list of the values in thresholdViolations,

<JSONPath>$.thresholdViolations.*</JSONPath>

thus:

["Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0","Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"]

but then i need to extract each of the items in the list into separate variables. The reason for all of this is that eventually, I'm going to need to create a payload like this:

[{"name":"Count0","value":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0"},{"name":"Count1","value":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"}]

Or also acceptable:

[{"name":"0 - Count","value":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=403 Trigger Value=7 Violation=sustained above 5.0"},{"name":"1 - Count", "value":"Duration=5 minutes Proxy=ALL Region=us-east1 Status Code=404 Trigger Value=7 Violation=sustained above 5.0"}]
Solved Solved
2 2 782
1 ACCEPTED SOLUTION

I think you need to do that with JavaScript callout.

There's no capability in the ExtractVariables policy that extracts all the values within a particular sub-tree of JSON, each into a separate context variable. ExtractVariables works when you want to query a fixed number of known, static JSON paths. What you want is something different. You want to map a subtree into something of a different shape. It's a JSON-to-JSON transform, and that's easy to do with JavaScript.

var value = context.getVariable('whatever');
var violations = value.thresholdViolations;
var result = Object.keys(violations).map(function (prop) {
      return { name: prop, value: violations[prop]};
    });
context.setVariable('result', JSON.stringify(result));

View solution in original post

2 REPLIES 2

I think you need to do that with JavaScript callout.

There's no capability in the ExtractVariables policy that extracts all the values within a particular sub-tree of JSON, each into a separate context variable. ExtractVariables works when you want to query a fixed number of known, static JSON paths. What you want is something different. You want to map a subtree into something of a different shape. It's a JSON-to-JSON transform, and that's easy to do with JavaScript.

var value = context.getVariable('whatever');
var violations = value.thresholdViolations;
var result = Object.keys(violations).map(function (prop) {
      return { name: prop, value: violations[prop]};
    });
context.setVariable('result', JSON.stringify(result));

That's what I suspected, and the direction I was heading. Thank you!