1

I have JSON object and i want to check key is set in that JSON object

Here is JSON object

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

If JSON Object like this here you can see Child is not exist there then how to check this

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18"
        }
    }
}

I have tried

if(Data_Array.Private.Price.Child[0].Price != "undefined"){
    ...
}

But it is showing me this error

Uncaught TypeError: Cannot read property

I am not be able to find out what should i do.

User97798
  • 634
  • 1
  • 5
  • 24
  • 2
    http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript – Hulothe Aug 22 '16 at 18:40

1 Answers1

0

check it out you are missing brace and I think undefined should not be a string

var Data_Array = {
"Private": {
    "Price": {
        "Adult": "18",
        "Child": [{
            "FromAge": "0",
            "ToAge": "12",
            "Price": "10"
        }]
    }
  }
} 
if(Data_Array.Private.Price.Child[0].Price != undefined){
  document.writeln(Data_Array.Private.Price.Child[0].Price)    
}
Babajide Apata
  • 671
  • 6
  • 18