Not able to convert the array value to JSON
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 06:03 AM
Hi,
I am trying to convert the array value which I have got as an output for the below script to JSON but it is sending [obect,object] as output. Please review the below script and output and please help in fixing the issue
var score='';
var scoreJson=[];
var scanApp = new GlideAggregate('incident');
scanApp.addAggregate('COUNT');
scanApp.addEncodedQuery("state!=7^short_description!=NULL");
scanApp.groupBy('sys_created_by');
//scanApp.orderByDesc('sys_created_on');
//scanApp.groupBy('number');
scanApp.query();
gs.info(scanApp.getRowCount());
while(scanApp.next()){
var createdby=scanApp.sys_created_by.getDisplayValue();
var scoreApp1 = new GlideRecord('incident');
scoreApp1.addQuery('sys_created_by', createdby);
scoreApp1.setLimit(1);
scoreApp1.orderByDesc('sys_created_on');
scoreApp1.query();
if(scoreApp1.next()){
//gs.log(scoreApp1.sys_created_by +" " + scoreApp1.number + " " + scoreApp1.sys_created_on );
score=scoreApp1.number;
}
//gs.log(score);
scoreJson.push(score);
}
gs.info(scoreJson.toString());
var arr = scoreJson;
gs.log(arr);
var arr1 =JSON.stringify(arr);
gs.log(arr1);
var arr2 =JSON.parse(arr1);
gs.log(arr2);
output -
*** Script: 8
*** Script: INC0000601,INC0000041,INC0000036,INC0000033,INC0000047,INC0000050,INC0000055,INC0000004
*** Script: INC0000601,INC0000041,INC0000036,INC0000033,INC0000047,INC0000050,INC0000055,INC0000004
*** Script: [{},{},{},{},{},{},{},{}]
*** Script: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
- Labels:
-
Cost Management (ITSM)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 06:09 AM
Hi,
Your scoreJson must be an object, not an array.
Like:
var scoreJson = {};
And instead of .push(), you should use:
scoreJson.score = score;
Please mark my response as correct if this solved your issue.
Regards,
Ionut

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 06:18 AM
When you write
score=scoreApp1.number;
scoreJson.push(score);
you think you are pushing a String value onto your array, but you are actually pushing a GlideElement.
You need to write
score=scoreApp1.getValue("number");
scoreJson.push(score);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 06:20 AM
Added few comments. Please check
var score = '';
var scoreJson = [];
var scanApp = new GlideAggregate('incident');
scanApp.addAggregate('COUNT');
scanApp.addEncodedQuery("state!=7^short_description!=NULL");
scanApp.groupBy('sys_created_by');
scanApp.query();
gs.info("Scan App Count " + scanApp.getRowCount());
while (scanApp.next()) {
var createdby = scanApp.sys_created_by.getDisplayValue();
var scoreApp1 = new GlideRecord('incident');
scoreApp1.addQuery('sys_created_by', createdby);
scoreApp1.setLimit(1);
scoreApp1.orderByDesc('sys_created_on');
scoreApp1.query();
if (scoreApp1.next()) {
score = scoreApp1.number;
}
scoreJson.push({
"score": score
});
}
//Array of scoreJSON
//Sample values is [{"score":"INC00001"}, {"score":"INC00002"}]
gs.info("Final ScoreJSON Array " + scoreJson.toString());
var arr = {};
arr.result = scoreJson;
gs.log("arr JSON Object " + arr);
var arr1 = JSON.stringify(arr);
//Sample value of arr1 is {"result" : [{"score":"INC00001"}, {"score":"INC00002"}] }
gs.log("Stringified JSON arr1 is " + arr1);
var arr2 = JSON.parse(arr1);
// Object object
gs.log(arr2);
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 06:43 AM
Hi Vasantharajan,
I tried the above code and we are not getting the output. Below is the output which we have got for the above code .Can you pls suggest in fixing it.
*** Script: Scan App Count 8
*** Script: Final ScoreJSON Array [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
*** Script: arr JSON Object [object Object]
*** Script: Stringified JSON arr1 is {"result":[{"score":{}},{"score":{}},{"score":{}},{"score":{}},{"score":{}},{"score":{}},{"score":{}},{"score":{}}]}
*** Script: [object Object]