Blogger Ever

Check a JSON path using if statement in Javascript

To check weather the JSON object exist at a specific JSON path we use 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

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

2013-2016 Copyright Blogger E\ver . Coded, managed and founded by Mohammad Hamza (@hamzadhamiya) . Hosted on Blogger and fonts used are Raleway and Open Sans.