How to get each Incident and sys id from the below script array of JSON object

Not applicable

Please help to get each output value from below script.

{"categoryKey":"BR Ordering|Customer|abc","recentIncidents":[{"number":"INC4449296","sys_id":"8422274f1bd80210"},{"number":"INC4449297","sys_id":"2da2e34f1bd802106"}],"historicIncidents":[{"number":"INC4449127","sys_id":"494c610c1b480a10"},{"number":"INC4449130","sys_id":"fafda14c1b040a508"},{"number":"INC4449296","sys_id":"8422274f1bd802106"},{"number":"INC4449297","sys_id":"2da2e34f1bd80210"}]}

 

It should print output as below

category is : BR Ordering|Customer|abc

Recent Incident are : INC4449296 , INC4449297

Historic Incidents are : INC4449127,INC4449130,INC4449296,INC4449297

 

Regards,

Nikki

 

 

 

1 ACCEPTED SOLUTION

Dhanraj B
Giga Guru

Hi @Community Alums ,

 

Try the code I have pasted below.

 

var payload = {"categoryKey":"BR Ordering|Customer|abc","recentIncidents":[{"number":"INC4449296","sys_id":"8422274f1bd80210"},{"number":"INC4449297","sys_id":"2da2e34f1bd802106"}],"historicIncidents":[{"number":"INC4449127","sys_id":"494c610c1b480a10"},{"number":"INC4449130","sys_id":"fafda14c1b040a508"},{"number":"INC4449296","sys_id":"8422274f1bd802106"},{"number":"INC4449297","sys_id":"2da2e34f1bd80210"}]};
var category = payload.categoryKey;
var recentIncidentNum= [];
var recentIncidents = payload.recentIncidents;
for(i=0; i < recentIncidents.length; i++){
    recentIncidentNum.push(recentIncidents[i].number.toString());
}
var historicIncidentNum = [];
var historicIncident = payload.historicIncidents;
for(j=0; j < historicIncident.length; j++){
    historicIncidentNum.push(historicIncident[j].number.toString());
}
gs.info("Category is :"+ category);
gs.info("Recent Incidents are :"+ recentIncidentNum);
gs.info("Historic Incidents are :"+ historicIncidentNum);
Output:

Script: Category is :BR Ordering|Customer|abc
*** Script: Recent Incidents are :INC4449296,INC4449297
*** Script: Historic Incidents are :INC4449127,INC4449130,INC4449296,INC4449297

Please mark as helpful and accept as solution, if you found this code useful.

 

Regards,

Dhanraj.

View solution in original post

3 REPLIES 3

Danish Bhairag2
Tera Sage

Hi @Community Alums ,

 

Can u try something like below.

 

Lets say u r storing that JSON value in 

 

var jsonOutput = "that JSON value";

 

var resultOutput= JSON.parse(jsonOutput);

 

gs.info("category is : " +resultOutput.categoryKey + '\n'+ "Recent Incidents are :"+ resultOutput.recentIncidents + '\n'+ "Historic Incidents are :"+resultOutput.historicIncidents);

 

Thanks,

Danish

Dhanraj B
Giga Guru

Hi @Community Alums ,

 

Try the code I have pasted below.

 

var payload = {"categoryKey":"BR Ordering|Customer|abc","recentIncidents":[{"number":"INC4449296","sys_id":"8422274f1bd80210"},{"number":"INC4449297","sys_id":"2da2e34f1bd802106"}],"historicIncidents":[{"number":"INC4449127","sys_id":"494c610c1b480a10"},{"number":"INC4449130","sys_id":"fafda14c1b040a508"},{"number":"INC4449296","sys_id":"8422274f1bd802106"},{"number":"INC4449297","sys_id":"2da2e34f1bd80210"}]};
var category = payload.categoryKey;
var recentIncidentNum= [];
var recentIncidents = payload.recentIncidents;
for(i=0; i < recentIncidents.length; i++){
    recentIncidentNum.push(recentIncidents[i].number.toString());
}
var historicIncidentNum = [];
var historicIncident = payload.historicIncidents;
for(j=0; j < historicIncident.length; j++){
    historicIncidentNum.push(historicIncident[j].number.toString());
}
gs.info("Category is :"+ category);
gs.info("Recent Incidents are :"+ recentIncidentNum);
gs.info("Historic Incidents are :"+ historicIncidentNum);
Output:

Script: Category is :BR Ordering|Customer|abc
*** Script: Recent Incidents are :INC4449296,INC4449297
*** Script: Historic Incidents are :INC4449127,INC4449130,INC4449296,INC4449297

Please mark as helpful and accept as solution, if you found this code useful.

 

Regards,

Dhanraj.

Not applicable

Hi Dhanraj, Thank you! This helped me to get each values.