0

I have a JSON response that looks like this

[
    {
        "alias": "Server1", 
        "hostgroup_worst_service_state": "0", 
        "status_history": {
            "status": [
                "0", 
                "2", 
                "0", 
                "0"
            ]
        }
    }, 
    {
        "alias": "Server2", 
        "hostgroup_worst_service_state": "0", 
        "status_history": {
            "status": [
                "0", 
                "0", 
                "0", 
                "0"
            ]
        }
    }, 
    {
        "alias": "Server3", 
        "hostgroup_worst_service_state": "0", 
        "status_history": {
            "status": [
                "1", 
                "1", 
                "1", 
                "0"
            ]
        }
    }
]

I can easily access the alias and hostgroup_worst_state using the .each iterator function:

 $.each(data, function(i, item) {
   console.log(item.alias)
   console.log(item.hostgroup_worst_service_state)
}

However I struggle to have access to the status data. I would need to display them this way:

["0","2","0","0"]
["0","0","0","0"]
["1","1","1","1"]

These value will next be used to display graphs using jQuery Sparklines.

Thanks for your help,

2
  • Hum. Did you look at the many related questions ? If so, what's the problem ? Commented Sep 11, 2012 at 14:11
  • 1
    does item.status_history.status not give you an array? Commented Sep 11, 2012 at 14:13

3 Answers 3

3

You have an array of objects, where each object has a property called status_history, which points to another object that has a property called status, which contains your required array. Thus, to access this array, use the following:

$.each(data, function () { 
  console.log(this.status_history.status); 
});

Note that this inside .each() refers to each object/value you iterate. It's the equivalent of using the following:

$.each(data, function (i, item) { 
  console.log(item.status_history.status); 
});

Also, if you want an array of arrays with the given status data, you could use $.map():

var arr = $.map(data, function (el) { 
  return [ el.status_history.status ];
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi guys, Thanks. I was on the right way but my error was somewhere else. However thumbs up for the explanation. It cleared my doubts and solve my problem to some extend.
1

Result will contain the required arrays.

var result = [];

for(var i = 0; i < data.length; i++) {
    result.push(data[i].status_history.status);
}

Comments

0

take your data to an element , say data then

for(i=0;i<data.length;i++)
{
 console.log(data.status_history.status);
}

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.