0

I have this dynamic json object

{
   "servers":[
      {
         "comp1":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"95.61"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.5"
               },
               "ip":{
                  "0":"192.168.0.1"
               }
            }
         }
      },
      {
         "comp2":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"88.39"
               },
               "ip":{
                  "0":"198.168.1.1"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.88"
               },
               "ip":{
                  "0":"192.168.0.1"
               }
            }
         }
      },
      {
         "comp3":{
            "server1":{
               "status":"none",
               "ar":"none",
               "ip":"none"
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.97"
               },
               "ip":{
                  "0":"http:\/\/122.01.125.107"
               }
            }
         }
      },
      {
         "comp4":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"95.64"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"95.65"
               },
               "ip":{
                  "0":"192.168.1.2"
               }
            }
         }
      },
      {
         "comp5":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"71.92"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"98.89"
               },
               "ip":{
                  "0":"192.168.0.3"
               }
            }
         }
      }
   ]
}

and I tried to parse it with $.each (refer below)

$.ajax({
        url:"/server-monitoring/load-servers",
        type:'post',
        dataType:'json',
        success:function(e){
            if(e.success){
                $.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    console.log(value.server1.status);
                });
            }
        }
    });

but unfortunately and sadly, it returns me an error (refer below)

Uncaught TypeError: Cannot read property 'status' of undefined

any help, ideas, suggestions, recommendation, clues please?

10
  • 2
    you are trying to loop a json. json is a string. you need to parse it to a object and then loop it Commented Feb 26, 2016 at 7:18
  • And why are you doing e.success? are you trying to check if the ajax call was a success?? or the message from the server has a success in it? Commented Feb 26, 2016 at 7:19
  • Because each object in the array has a primary key called comp[num] which is an object and within that is the server object which contains the status property. Commented Feb 26, 2016 at 7:20
  • 2
    @Reddy, jQuery auto-parses the JSON if the dataType has been set correctly. You don't need to parse it again. Commented Feb 26, 2016 at 7:21
  • @Andy . thanks for the info, Commented Feb 26, 2016 at 7:22

3 Answers 3

2

From your structure of response, following will work

$.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    console.log(value['comp'+(index+1)].server1.status);
                });

Gives output as

boxup
boxup
none
boxup
boxup

Edit:

After clarification in comment and assuming there will be any single key Below can work

$.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    var key = Object.keys(value)[0];
                    console.log(value[key].server1.status);
                }); 
Sign up to request clarification or add additional context in comments.

5 Comments

Nice @Andy +1, Reddy's comment in that direction..Cool!!
actually there's a circumstances where its not comp1, comp2 and etc.. so I cant parse it like console.log(value['comp'+(index+1)].server1.status); because the comp is dynamic e.g. it can be "adelle" and not comp
Is there will be single key?
ahahah thats what even I am wondering.. I think its multiple.. and his server1 can also be multiple.
guys, the only dynamic is they key object e.g. comp1, comp2, and etc. and the rest is static inside the json response.
1

How about this code. It will handle dynamic keys, number of objects and dynamic inner objects too.

$.each(data.servers, function(key, value) {
   $.each(this,function(key,value){
       var parentObj = key;      
       $.each(value,function(key,value){
          console.log(parentObj + '------'+key + '-----'+value.status);       
       });
   });
});

Here is a Working Fiddle

And here is the output.

comp1------server1-----boxup
comp1------server2-----boxup
comp2------server1-----boxup
comp2------server2-----boxup
comp3------server1-----none
comp3------server2-----boxup
comp4------server1-----boxup
comp4------server2-----boxup
comp5------server1-----boxup
comp5------server2-----boxup

Comments

0

By Observing your JSON structure, I believe that each server object (in servers array) has one property like comp1, comp2 ... which is not known.

Following code works.

   var comp = {}; //Just a temp variable to hold dynamic comp property
                  // value

   $.each(v.servers,function(i,v){ 
      //Loop through each key in server object to find first property.
      for(var key in v) {
        //Make sure its objects own property, just to be safe.
        if(v.hasOwnProperty(key)) {
            // Fetch our comp (assumed, you can also match key 
            // and make sure it starts with comp ...
            comp = v[key];
            break;
        }
      }
      //As we have our comp object, now we can access server1, server2 as shown below
      console.log(comp.server1.status);
    });

Hope this helps !! Let me know if you need further assistance .... :)

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.