- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2021 02:06 AM
Hi All,
I am trying to check if element exists in Nested JSON object but it's not working. I have tried hasOwnProperty method and it's working for direct JSON but not working for nested one as show below.
var response='{"short_description":"Test","description":{"status":"New"}}';
var obj = new global.JSON().decode(response)
if (obj.hasOwnProperty('status'))
{
gs.print('found');
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2021 03:00 AM
For this you have to create a recursive function. Try below code.
var response='{"short_description":"Test","description":{"status":"New"}}';
var obj = new global.JSON().decode(response);
gs.print(CheckKey('status',obj));
function CheckKey(key,obj){
for (var name in obj) {
if(typeof obj[name]=='object'){
var newObj=obj[name];
return CheckKey(key,newObj)
}
else if(name==key){
return true;
}
}
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2021 04:56 AM
My bad, in a hurry I forgot to declare the object before invoking a function of a class.
Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2021 03:01 AM
Here's an answer feom stackoverflow: https://stackoverflow.com/questions/17957564/how-do-i-check-to-see-if-an-object-or-its-children-sub-...
One of the keys to programming is to try to identify the generic case of whatever problem you're trying to solve. In this instance, while you're dealing with JSON, the solution is all just about objects and "child" objects. A web search that includes "JSON" might not have any good results, but the above is the first hit I got googling "javascript does object or its children have property".
Frank