My current code for the service now rest outbound call is functioning correctly. However, I am facing issues while trying to parse the JSON body in the second REST call and fetch values in the desired table format.
try {
var r = new sn_ws.RESTMessageV2('test', 'post');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print(response.getBody());
gs.print(response.getStatusCode());
var JsonObject = JSON.parse(responseBody);
var sid = JsonObject.sid;
gs.print(sid);
var r1 = new sn_ws.RESTMessageV2('gateways', 'POST' );
r1.setRequestHeader("X-chkp-sid",sid );
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus = response1.getStatusCode()
gs.print(response1.getBody());
var parser = new JSONParser();
var jsonObj = parser.parse(responseBody1);
var outputString = 'Name Domain-Name OS Cluster-Members UID';
for (var i=0; i < jsonObj.objects.length; i++) {
var obj = jsonObj.objects[i];
outputString = outputString + "\n" + obj.name + ", " + obj.domain.name + ", " + obj["operating-system"] + ", " + obj["cluster-member-names"] + ", " + obj.uid;
}
gs.log(outputString);
}
catch(ex) {
var message = ex.message;
}
The expected output should be like this:
However, the actual output that I am getting is:
*** Script: Name Domain-Name OS Cluster-Members UID
clus-cn-1, dom-cn-1, Gaia, dev-cn-c1,dev-cn-c2, 16b96771-d13a-4c11-b457-9c0861aaf3c8
clus-cn-2, dom-cn-2, Gaia, dev-cn-c3,dev-cn-c4, 385c0a22-275c-4a70-9489-2b6ccd191eb8
dev-cn-c1, dom-cn-1, undefined, undefined, 4652da03-0e2f-4a0a-880c-338396be0818
dev-cn-c2, dom-cn-1, undefined, undefined, 2dbbbfd4-80ac-4fd9-b792-0a5b468c6409
dev-cn-c3, dom-cn-2, undefined, undefined, 6037e235-f19a-49ac-a39c-4889d979acbe
dev-cn-c4, dom-cn-2, undefined, undefined, 022c44df-9271-46db-b782-da084c476dd2
dom_cn_1_Server, dom-cn-1, Unknown OS, undefined, 4f417c60-3541-4c0f-a542-9a100d857077
dom_cn_2_Server, dom-cn-2, Unknown OS, undefined, cebecd33-1efb-4530-9de9-7c666e588ee7
I need to prevent the displayed values after the first two lines.
This is a sample of the provided JSON data:
{
"objects" : [ {
"uid" : "16b96771-d13a-4c11-b457-9c0861aaf3c8",
"name" : "clus-cn-1",
"type" : "CpmiGatewayCluster",
"domain" : {
"uid" : "16280a32-183a-4050-b698-f59dbe488da6",
"name" : "dom-cn-1",
"domain-type" : "domain"
},
...
}