pool
pool

Reputation: 91

JavaScript how to check if a property is missing in a json

i'm having some trouble in finding a way to check if in a parsed json object is present a property. For example in my js i have this line of code:

jsonArray = JSON.parse(jsonResponse)

I wanna check if in the jsonArray object there is the property media.

For example if my json look like this one:

Object0 {hashtags: Array[0], 
         symbols: Array[0],
         user_mentions: Array[1], 
         urls: Array[0]}
Object1 {hashtags: Array[1], 
        symbols: Array[0], 
        user_mentions: Array[0],
        urls: Array[1], 
        media: Array[1]}

i wanna check if Object0 has property media and if Object1 has property media.

Thank's

Upvotes: 4

Views: 14266

Answers (6)

Mitch Ross
Mitch Ross

Reputation: 1

Late to the party, but I had the same question. An answer for beginners like me:

If statements, testing for just a value that's undefined, is the same as false.

The example code below returns:

"yes animals"

"yes animals.dog"

"nope --- animals.dog.fleas"

    const animals = {
      dog: {
        name: 'Rover'
      }
    };
    
    if ( animals ) {
      console.log( "yes animals" );
    }
    
    if ( animals.dog ) {
      console.log( "yes animals.dog" );
    }
    
    if ( animals.dog.fleas ) {
      console.log( "yes animals.dog.fleas" )
    } else {
      console.log( "nope --- animals.dog.fleas" )
    }

Upvotes: 0

Himanshu Tanwar
Himanshu Tanwar

Reputation: 906

if(object0['media'] !== undefined){
//media property present
}else{
//media property not present
}

Do same for object1

Upvotes: 0

AmmarCSE
AmmarCSE

Reputation: 30557

Check if it is undefined using typeof() like

if(typeof(Object0['media']) == 'undefined'){
}

Upvotes: 0

Tushar
Tushar

Reputation: 87203

You can use hasOwnProperty:

if (Object0.hasOwnProperty('media')) {
    // Object0.media
}

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property. Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.

Upvotes: 8

whatoncewaslost
whatoncewaslost

Reputation: 2256

If you've already parsed it, it should just be

jsonArray.media

or

jsonArray["media"]

to call the property. If you don't need to get the property, but just check it, you can do:

jsonArray.media === 'undefined'

Upvotes: 0

ssube
ssube

Reputation: 48277

Once you've parsed the JSON, it becomes a normal JavaScript object and you should use the hasOwnProperty method to check whether the property exists.

Since JSON objects don't have a property or any complicated inheritance like other JS objects can, all properties that exist will be their own and return true with a hasOwnProperty check:

var data = '...';
var obj = JSON.parse(data);
if (obj.hasOwnProperty('foo')) {
  ...
}

Upvotes: 1

Related Questions