About

Labels

slider

Recent

Navigation

Check a JSON path using if statement in Javascript

Check a JSON path using if statement in Javascript

While finding out a solution for a simple but a concerned problem that is to check weather a given JSON path will return something or the path doesn't exist. To find this out we have a solutions to tackle the situation.

By using If condition

If and else condition is used for  purposes that also includes checking the JSON path. The example I'll be using here is of Facebook shares.

In on of my previous articles I shared of parsing Facebook data for a web page, If the page do not have any shares then the JSON data do not contain any shares. 

Taking data as argument

if(data.shares){
var commentcount=data.shares;
}else{
var commentcount="No Shares";
}
See the Pen zxYovy by Mohammad Hamza Dhamiya (@hamzadhamiya) on CodePen.
Share
Banner

Muhammad Hamza

Themelet provides the best in market today. We work hard to make the clean, modern and SEO friendly blogger templates.

Post A Comment:

0 comments: