16

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.

5
  • 3
    Data_Array.Private.Price.hasOwnProperty("Child") Commented Aug 24, 2016 at 20:51
  • 4
    FYI: You do not have a "JSON object." This is a widespread misnomer. JSON stands for "Javascript Object Notation." So any valid JSON is valid Javascript. What you have there is an object. JSON is a text format. Commented Aug 24, 2016 at 20:53
  • There is no such thing as a "JSON Object" Commented Aug 24, 2016 at 21:08
  • See also What is the difference between JSON and Object Literal Notation? Commented Aug 24, 2016 at 21:10
  • @MikeC, i agreed your word and i kept it in my mind a big thanks for information. Commented Aug 25, 2016 at 16:52

4 Answers 4

30

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)

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

3 Comments

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';
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
Great!, Good explanation.
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 } 

3 Comments

Will throw an exception if Private is not a property
right, but the question was about Child to test.
Just wanted to inform as it seems OP does not understand why those exceptions arise
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! } } 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.