- 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 02:42 AM
Since you wanted to check the nested object you can try the below method.
var response='{"short_description":"Test","description":{"status":"New"}}';
var obj = new global.JSON().decode(response)
function checkKey(obj, key) {
var t, k;
if(key in obj){
return obj[key];
}
else{
for (k in obj){
t = obj[k]
if(typeof t === "object" && !Array.isArray(t) && t !== null){
return checkKey(t, key);
}
}
}
return false
}
gs.print(checkKey(obj, "status"));
// The return value will be false if the element does not exist in the JSON//
- 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:32 AM
I tried what you given from background script and it worked well but same is not working within script includes. It's returning me undefined.
Calling Script Include
gs.log(INT_checkKeyValidation.checkKey("impact",obj));
Main script include having the logic
var INT_checkKeyValidation = Class.create();
INT_checkKeyValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkKey: function(key, obj) {
for (var name in obj) {
if (typeof obj[name] == 'object') {
var newObj = obj[name];
return this.checkKey(key, newObj);
} else if (name == key) {
return obj[key] + '|' + name;
}
}
return false;
},
type: 'INT_checkKeyValidation'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2021 04:42 AM
var aa=new INT_checkKeyValidation();
var response='{"short_description":"Test","description":{"status":"New"}}';
var obj = new global.JSON().decode(response);
gs.print(aa.checkKey('status',obj));
Above working for me