JSON validator function in a simple javascript

We all know javascript is a loosely coupled scripting language. Sometime javascript acts crazy with null checks. Especially when we are working with JSON objects.

In one of the scenario where nodes are not permanent members then we need to check all possible nulls throughout the path.

Ex. jsonObj.var1.var2.var3
so null check would be like

if(typeof jsonObj != undefined && typeof jsonObj.var1 != undefined && typeof jsonObj.var1.var2 != undefined && typeof jsonObj.var1.var2.var3 != undefined)

There are many ways to find a null check on this object. Likewise I found my own way of finding it via a function call. Created a method which will handle this job for me.

I will have to just pass the parent object and the path underneath it.

Code snippet:

Ex.
if( isValidJSONPath(jsonObj, 'var1.var2.var3') )

Comments