Anson Aştepta
Anson Aştepta

Reputation: 1145

Checker null value of JSON by jquery

for example i acquire some json data like below

     errorCode: null,
        message: "Success",
        result: {
        keywordData: null, 
        },
       totalRecord: 1,
    checkAccess: true,
   token: xxxxx

As you see the keywordData is null.

And i am having a if statment in my JS like below

if(Object.keys(result.keywordData.length !== 0)){

  //do something

}

but it's not working, how may i check null value?

full JSON:

  {
    errorCode: null,
    message: "Success",
    result: {
    keywordData: null,
    keywords: null,
    lastSyncDate: 1465168445000,
    profileId: 129,
    overallTrend: null
    },
    totalRecord: 1,
    checkAccess: true,
    token: "123"
    }

Upvotes: 0

Views: 529

Answers (2)

Vuong
Vuong

Reputation: 637

var a = {
  keywordData: null
};

if(a.keywordData !== null) {
  console.log('false');
} else {
  console.log('true');
}

Upvotes: 1

Soubhik Mondal
Soubhik Mondal

Reputation: 2666

You've not provided the full structure of the object, so I'm assuming this is what it looks like:

var object = {
    errorCode: null,
    message: "Success",
    result: {
        keywordData: null,
    },
    totalRecord: 1,
    checkAccess: true,
    token: xxxxx
};

If this is indeed the structure of the object, then you can simply do this:

if (object.result.keywordData) {
    // do something
}

Upvotes: 1

Related Questions