0

So i'm using Azure Application Insights REST API to get data from my web app to construct it into a graph to be displayed on a dashboard.

The json that comes back is as follows:

{
  "value": {
    "start": "2017-08-07T23:01:50.847Z",
    "end": "2017-08-08T11:01:50.847Z",
    "interval": "PT1H",
    "segments": [
      {
        "start": "2017-08-07T23:01:50.847Z",
        "end": "2017-08-08T00:00:00.000Z",
        "requests/count": {
          "sum": 317
        }
      },
      {
        "start": "2017-08-08T00:00:00.000Z",
        "end": "2017-08-08T01:00:00.000Z",
        "requests/count": {
          "sum": 332
        }
      },
      {
        "start": "2017-08-08T01:00:00.000Z",
        "end": "2017-08-08T02:00:00.000Z",
        "requests/count": {
          "sum": 337
        }
      },
      {
        "start": "2017-08-08T02:00:00.000Z",
        "end": "2017-08-08T03:00:00.000Z",
        "requests/count": {
          "sum": 326
        }
      }
    ]
  }
}

I need to get the "end" and "sum" values in "segments" to construct an array like so since the graph api requires the array to be in this format:

[
    ["2017-08-08T00:00:00.000Z", 317], 
    ["2017-08-08T01:00:00.000Z", 332],
    ["2017-08-08T02:00:00.000Z", 337],
    ["2017-08-08T03:00:00.000Z", 326]

]

How can i achieve this?

3 Answers 3

1

If you prefer some modern JavaScript, you can also do something like :

// “res” is your API response
const newArray = res.value.segments.reduce((acc, val) => {
  return [...acc, [val.end, val['requests/count'].sum]];
}, []);

Learn more about reduce magic.

I hope it will help !

Sign up to request clarification or add additional context in comments.

Comments

0
var myArray = resultFromAPI.value.segments.map(function(segment) {
  return [segment.end, segment["requests/count"].sum];
});

should do the trick

Comments

0

Use javascript map. Something like:

t.value.segments.map(function(x) { return [ x.start, x['requests/count'].sum] })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.