How can i iterate json response array and add values on selected property

Not applicable

How to add all value for message_count and error_count in two variables and need to include both in assign message payload ?

Please find the final JSON response -

{
  "environments": [{
    "dimensions": [{
      "metrics": [{
        "name": "sum(message_count)",
        "values": [{
          "timestamp": 1492473600000,
          "value": "1.0"
        }, {
          "timestamp": 1492387200000,
          "value": "878.0"
        }, {
          "timestamp": 1492300800000,
          "value": "115.0"
        }, {
          "timestamp": 1492214400000,
          "value": "1161.0"
        }, {
          "timestamp": 1492128000000,
          "value": "305.0"
        }, {
          "timestamp": 1492041600000,
          "value": "540.0"
        }, {
          "timestamp": 1491955200000,
          "value": "134.0"
        }]
      }, {
        "name": "sum(error_count)",
        "values": [{
          "timestamp": 1492473600000,
          "value": "0.0"
        }, {
          "timestamp": 1492387200000,
          "value": "366.0"
        }, {
          "timestamp": 1492300800000,
          "value": "0.0"
        }, {
          "timestamp": 1492214400000,
          "value": "36.0"
        },


Solved Solved
1 8 9,914
1 ACCEPTED SOLUTION

@Aritra Datta, Try the below JS code.

var response = JSON.parse(context.getVariable("response.content"));
var metrics = response.environments[0].dimensions[0].metrics;
var errorTotal = 0;
var msgTotal = 0;

for (var i=0; i<metrics.length; i++) {
var metric = metrics[i];
var type = metric.name;
var values = metric.values;
var value = 0;
for (var j=0; j<values.length; j++) {
value += Number(values[j].value);
}

if ("sum(message_count)".equalsIgnoreCase(type)) {
msgTotal = value;
} else if ("sum(error_count)".equalsIgnoreCase(type)) {
errorTotal = value;
}
}
context.setVariable("errorTotal",errorTotal);
context.setVariable("msgTotal",msgTotal);

View solution in original post

8 REPLIES 8

You can iterate through this list in a JavaScript policy and set two context variables (message count and error count) for further processing.

Hi Sudheendra, I tried but it's not working; please find my code below and let me know what I am doing wrong -

var response = JSON.parse(context.getVariable("response.content"));
var arrayLength = response.environments.dimensions.metrics.values;
var errorTotal = 0;
for (var i=0; i<arrayLength.length; i++)
{
errorTotal = errorTotal + response.environments.dimensions.metrics.values.value[i];
}
context.setVariable("errorTotal",JSON.stringify(errorTotal));

I am getting error as below -

Javascript runtime error: \"TypeError: Cannot read property \"metrics\" from undefined.

Environments, metrics & dimensions are all JSON array... So you will have to do something like response.environments[0].dimensions[0].metrics[0].values....

@Aritra Datta, Try the below JS code.

var response = JSON.parse(context.getVariable("response.content"));
var metrics = response.environments[0].dimensions[0].metrics;
var errorTotal = 0;
var msgTotal = 0;

for (var i=0; i<metrics.length; i++) {
var metric = metrics[i];
var type = metric.name;
var values = metric.values;
var value = 0;
for (var j=0; j<values.length; j++) {
value += Number(values[j].value);
}

if ("sum(message_count)".equalsIgnoreCase(type)) {
msgTotal = value;
} else if ("sum(error_count)".equalsIgnoreCase(type)) {
errorTotal = value;
}
}
context.setVariable("errorTotal",errorTotal);
context.setVariable("msgTotal",msgTotal);

Really appreciate. Thanks a lot. It works perfectly.

I'm not clear on what you would like to do.

Do you want to sum all the message_count and error_count values? And then set a context variable for that aggregate sum? You should be able to do that with a set of nested foreach loops.

I tried it like this, it seems to work outside of Trireme:

var x = JSON.parse(context.getVariable("response.content"));

function varName(env, dim, metric) {
  return ['aggregate', env, dim, metric].join('_')
    .replace('(', '-')
    .replace(')', '');
}

x.environments.forEach(function(env, env_ix) {
  env.dimensions.forEach(function(dim, dim_ix) {
    dim.metrics.forEach(function(metric, m_ix) {
      var sum = metric.values.reduce(function(accumulator, currentValue){
            return accumulator + parseFloat(currentValue.value);
          }, 0);
      context.setVariable(varName(env.name, dim_ix, metric.name), sum);
    });
  });
});

// result: context has variables like this:
// {
//  "aggregate_test_0_sum-message_count": 3134,
//  "aggregate_test_0_sum-error_count": 1381
// }

I tested this outside of Edge. I don't know for certain that the array.reduce function is available in trireme. If not, it's easy to provide as a polyfill.

Thank you for your help. I have to get these two counts and send to assign message policy payload. It's an internal requirement.