How to check Object is Empty Or not
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 01:44 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 01:45 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 03:03 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 03:08 PM
Yeah. Because you pushed an object in the array. I would do something like this.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2024 06:19 AM
Okay, Let me try this method