Isaac
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.
How can I check if a node exist using javaScript before attempting to use it?
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;"
}
}}
How can I check if a node exist using javaScript before attempting to use it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
check this:
https://jsfiddle.net/Lj24d5op/
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!");
if it is string, you can first convert it into JSON object and use same solution above...
demo
https://jsfiddle.net/snm6e6hh/
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!");
demo
https://jsfiddle.net/snm6e6hh/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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.
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.
ASKER
I don't see the difference between your two suggestions.