I receive the following JSON as a response
{
"user_details": [
{
"Name": "Mark"
},
{
"Age": "35"
},
{
"Gender": "Male"
},
{
"Country": "US"
}]
}
I am parsing this JSON as shown below
var ajaxresponse = response.user_details;
if (ajaxresponse.length > 0)
{
var Name = ajaxresponse[0].Name;
var Age = ajaxresponse[1].Age;
var Gender = ajaxresponse[2].Gender;
var Country = ajaxresponse[3].Country;
console.log(Name);
}
This is working fine .
My question is , if any one of the key is missing in the JSON for example "Name" is missing , its breaking and i am getting undefined
Is it possible to check if exists and then retrive ?
https://jsfiddle.net/o2gxgz9r/9078/
With respect to the answers i modified my json to
{
"user_details": [
{
"Name": "Mark",
"Age": "35",
"Gender": "Male",
"Country": "US"
}
]
}
But hasOwnProperty is not working ?
please see this fiddle