Reputation: 32331
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
https://jsfiddle.net/o2gxgz9r/9085/
Upvotes: 0
Views: 71
Reputation: 1020
the below code you can check the property present in JSON Array and also get the value of the property from Array
array.forEach(item=>{
if(item.hasOwnProperty(propertyname)){
if(item[propertyname]){
resultArray.push(item[propertyname])
}
}
})
Upvotes: 0
Reputation: 13069
Make it a bit more general try something like this which will iterate through the user_details
array setting properties for each item.
var ajaxresponse = response.user_details;
var user_details = ajaxresponse.reduce(function(details, detail){
// details is the object we'll populate and will get assigned to user_details
// detail is the current user detail object - Name/Age/Gender/Country
// get property name for this "detail"
var propertyName = Object.getOwnPropertyNames(detail)[0];
// set the property and value for the current detail object
details[propertyName] = detail[propertyName];
// return the updated details object for the next iteration
return details;
}, {});
console.log(user_details.Name);
This has the added bonus that any new properties in the result set will be handled automatically.
Upvotes: 0
Reputation: 4543
Firstly this is a wrong way to send data as a response from whatever source it is coming.
Ideal way should be an object map or a vector as given below:
user_details: {
name: "Mark",
age: 35,
gender: "male",
country: "USA"
}
Secondly, if you want a solution for the data structure you are getting, you will have to actually traverse the array for each item and see if a property exists on it or not.
var arr = ajaxResponse;
var name,age,country,age;
arr.forEach(function(item){
if(item.hasOwnProperty('name')){
name = item.name
}
//similarly for other objects in the array
));
Upvotes: 2
Reputation: 145
use javascript's hasOwnProperty function,
if(json_object.hasOwnProperty('name')){
//do struff
}
Here
if (ajaxresponse.length > 0)
{
if(ajaxresponse.hasOwnProperty("Name"))
{
var Name = ajaxresponse[0].Name;
var Age = ajaxresponse[1].Age;
var Gender = ajaxresponse[2].Gender;
var Country = ajaxresponse[3].Country;
console.log(Name);
}
}
Upvotes: 0