15

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 as you can see Child is not exist, 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
  • 3
    `Data_Array.Private.Price.hasOwnProperty("Child")` – VLAZ Aug 24 '16 at 20:51
  • 4
    FYI: You do *not* have a "JSON object." This is a widespread misnomer. [JSON](http://www.json.org/) stands for "Javascript Object Notation." So any valid JSON is valid Javascript. What you have there is an *object*. JSON is a text format. – Mike Cluck Aug 24 '16 at 20:53
  • [There is no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Felix Kling Aug 24 '16 at 21:08
  • See also [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/q/2904131/218196) – Felix Kling Aug 24 '16 at 21:10
  • @MikeC, i agreed your word and i kept it in my mind a big thanks for information. – User97798 Aug 25 '16 at 16:52

4 Answers4

29

var json = {key1: 'value1', key2: 'value2'}

"key1" in json ? console.log('key exists') : console.log('unknown key')

"key3" in json ? console.log('key exists') : console.log('unknown key')

for child key

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

'Child' in Data_Array.Private.Price ? console.log('Child detected') : console.log('Child missing')

create variable child

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

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)

if there is no child

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

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)
  • This works fine but could you help me how it works `var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'` VS `var child = 'Child' in (Data_Array.Private.Price && Data_Array.Private.Price.Child[0]) ? Data_Array.Private.Price.Child[0] : 'there is no child';` – User97798 Aug 25 '16 at 10:12
  • 2
    In expression var a = b && c || d if b is true, c is returned and d is skipped, so a = c, or if b is false, c is skipped and d is returned, so a = d –  Aug 25 '16 at 10:24
  • Great!, Good explanation. – User97798 Aug 25 '16 at 14:50
3

You could use the in operator for an object.

The in operator returns true if the specified property is in the specified object.

if ('Child' in Data_Array.Private.Price) {
    // more code
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Trying to get a property off of an object that is not defined will raise an exception. You need to check each property for existence (and type) throughout the chain (unless you are sure of the structure).

Using Lodash:

if(_.has(Data_Array, 'Private.Price.Child')) {
    if(Array.isArray(Data_Array.Private.Price.Child) && Data_Array.Private.Price.Child.length && Data_Array.Private.Price.Child[0].Price) {
        // Its has a price!
    }
}
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57
0

Check if Child is undefined

if (typeof Data_Array.Private.Price.Child!== "undefined") {
    ...
}

Or you can use in:

if ("Child" in Data_Array.Private.Price) {
    ...
}

Or you can use _.isUndefined(Data_Array.Private.Price.Child) of underscore.js

rm4
  • 711
  • 4
  • 15