Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Not able to convert the array value to JSON

Maradani Revat1
Tera Contributor

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]

5 REPLIES 5

Could you please use the below script and check.

Note: Made changes on the below code from score to score.toString()

scoreJson.push({
        "score": score
    });
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.toString()
    });
}

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