Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

check if json node exist

Hello,

I was given an API by my client and as I was working with the API (several weeks), I notice that sometimes a node that was there before wouldn't be there at another time.  
For example, "widget.window.title" could be missing next week.

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}  

Open in new window




How can I check if a node exist using javaScript before attempting to use it?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Hi,

I don't see the difference between your two suggestions.
check this:

https://jsfiddle.net/Lj24d5op/

if (json.widget.window.title) alert("json.widget.window.title is set!");
else alert("json.widget.window.title is NOT set!");

if (json.widget.window.header) alert("json.widget.window.header is set!");
else alert("json.widget.window.header is NOT set!");

Open in new window

if it is string, you can first convert it into JSON object and use same solution above...

var jsonStr = '{  "widget": {    "debug": "on",    "window": {      "title": "Sample Konfabulator Widget",      "name": "main_window",      "width": 500,      "height": 500    },    "image": {      "src": "Images/Sun.png",      "name": "sun1",      "hOffset": 250,      "vOffset": 250,      "alignment": "center"    },    "text": {      "data": "Click Here",      "size": 36,      "style": "bold",      "name": "text1",      "hOffset": 250,      "vOffset": 100,      "alignment": "center",      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"    }  }}';

var jsonObj = JSON.parse(jsonStr);

if (jsonObj.widget.window.title) alert("jsonObj.widget.window.title is set!");
else alert("json.widget.window.title is NOT set!");

if (jsonObj.widget.window.header) alert("jsonObj.widget.window.header is set!");
else alert("jsonObj.widget.window.header is NOT set!");

Open in new window


demo
https://jsfiddle.net/snm6e6hh/
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> probably he meant this:..
Indeed! I apologize for the confusion.  I first wrote the second if-clause and then did a copy-paste above it.  I obviously forgot to remove the first condition from the first if-clause.