Check if elements exists in Nested JSON

Khanna Ji
Tera Guru

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');                      
}

 

@franktate @Chuck Tomasi @Mark Roethof - Tagged you to see if you can help

1 ACCEPTED SOLUTION

Upender Kumar
Mega Sage

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;
	}

View solution in original post

6 REPLIES 6

Akhil Kumar1
Mega Sage

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//

 

 

Upender Kumar
Mega Sage

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;
	}

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'
});

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