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

Ionut Carp
Giga Sage

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

Giles Lewis
Giga Guru

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

Vasantharajan N
Tera Sage
Tera Sage

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

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]