2

I have this JSON response/

{  
   "phweb":{  
      "name":"PH Web Server",
      "cpu":"36.7",
      "ram":"33.0",
      "hdd":"43.0",
      "os":{  
         "osup":"2 days, 23 hours, 5 minutes and 34 seconds",
         "osstat":"0"
      },
      "apache":{  
         "apachestat":"0",
         "apacheup":"33 days, 2 hours, 9 minutes and 16 seconds"
      },
      "sql":{  
         "sqlstat":"0",
         "sqlup":"154 days, 2 hours, 16 minutes and 53 seconds"
      }
   },
   "phdbm":{  
      "name":"PH DBM Server",
      "cpu":"15.9",
      "ram":"10.7",
      "hdd":"23.7",
      "os":{  
         "osup":"2 days, 23 hours, 6 minutes and 21 seconds",
         "osstat":"0"
      },
      "apache":{  
         "apachestat":"0",
         "apacheup":"103 days, 4 hours, 43 minutes and 56 seconds"
      },
      "sql":{  
         "sqlstat":"0",
         "sqlup":"12 days, 4 hours, 43 minutes and 42 seconds"
      }
   }
}

and I could fetch each value using

$.each(response, function(index, value){
    console.log(index + " " + value);
});

How can I fetch the value of a response value that has a sub array values e.g.

      "os":{  
         "osup":"2 days, 23 hours, 6 minutes and 21 seconds",
         "osstat":"0"
      },

of the "phweb" index (assume i want to get the 'osup' from 'os' of 'phweb'? and how can I check if its empty or it did exist (to avoid errors)?

1
  • There's no arrays here, just objects. Therefore you can use response.phweb.os to access the nested object. It will be undefined if there was no matching property Commented Oct 12, 2015 at 9:44

1 Answer 1

1

First it is JSON object not JSON array.

You can directly access it using key.

Use variable name e.g obj and use . operator with key to value of that key.

In below example it is obj.phweb.os which means obj is JSON object having multiple key value pair including key phweb and same for phweb and os.

Keep In Mind: JSON object has key value and wrap in {} while array has index and wrap in [].

var obj = {  
   "phweb":{  
      "name":"PH Web Server",
      "cpu":"36.7",
      "ram":"33.0",
      "hdd":"43.0",
      "os":{  
         "osup":"2 days, 23 hours, 5 minutes and 34 seconds",
         "osstat":"0"
      },
      "apache":{  
         "apachestat":"0",
         "apacheup":"33 days, 2 hours, 9 minutes and 16 seconds"
      },
      "sql":{  
         "sqlstat":"0",
         "sqlup":"154 days, 2 hours, 16 minutes and 53 seconds"
      }
   },
   "phdbm":{  
      "name":"PH DBM Server",
      "cpu":"15.9",
      "ram":"10.7",
      "hdd":"23.7",
      "os":{  
         "osup":"2 days, 23 hours, 6 minutes and 21 seconds",
         "osstat":"0"
      },
      "apache":{  
         "apachestat":"0",
         "apacheup":"103 days, 4 hours, 43 minutes and 56 seconds"
      },
      "sql":{  
         "sqlstat":"0",
         "sqlup":"12 days, 4 hours, 43 minutes and 42 seconds"
      }
   }
}

console.log(obj.phweb.os);

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

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.