How to check Object is Empty Or not

Sironi
Kilo Sage

Hi All,

Can you please suggest me how to check Object is empty or not,
If JSON is empty then complier should not go till " return JSON.stringify(arr);" 

if JSON is not empty then complier  should touch return JSON.stringify(arr); line in Code.

 

issue: script include returning error message like "Empty JSON string", If JSON is empty then 

Script include :

{

var arr=[];
for(var i=0;i<list.length;i++)

obj={

name: list[i].name,

id:list[i].id

},

arr.push(obj);

}

return JSON.stringify(arr);

},


to control this I tried below method to check JSON is empty or not ? but not working

1. Object.Keys(JSON.stringify(arr)).length===0

2. if(JSON.stringify(arr)==='{}')

 

Please suggest me , how to fix this

7 REPLIES 7

SanjivMeher
Kilo Patron
Kilo Patron

You can use a condition to check the length

 

if (arr.length>0)

{

return JSON.stringify(arr);

}


Please mark this response as correct or helpful if it assisted you with your question.

hi Sanjiv, 

I tried arr.length , it is not working

 

var obj={}

var arr=[];

arr.push(obj);

g.info(arr.length); // Result it is giving 1

 

Yeah. Because you pushed an object in the array. I would do something like this.

 

var obj={name:"",id:""}

var arr=[];
if (obj.name) // Only if object has a value, push to array
arr.push(obj);

gs.info(arr.length);

Please mark this response as correct or helpful if it assisted you with your question.

Okay, Let me try this method