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.

JSON Help Looping through Object properties

triciav
Kilo Sage

I am running a restmessage to pull JSON from an API

I need to loop through the response and then the properteries of the response object.

Here I am getting the responseBody.length

How can I then do the same for the +response[i].risk.evidenceDetails[0].rule 

My risk evidence details could have 0 - 50 items.

find_real_file.png

Instead of having [0] for the evidenceDetails.rule I need to achieve the same thing that I did with the initial response.

response[i].risk.evidenceDetails[j].rule 

for(i=0;i<response.length;i++){
gs.info("TRICIA = " +response[i].risk.criticalityLabel + " name "+response[i].entity.name+ " id "+response[i].entity.id+ " score "+response[i].risk.score+ " rule "+response[i].risk.evidenceDetails[0].rule + " evidencestring "+response[i].risk.evidenceDetails[0].evidenceString);
}

for(j=0;j<response[i].risk.evidenceDetails.length;j++){
gs.info("TRICIA = " +response[i].risk.evidenceDetails[j]);

}

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

try

var responseBody = response.getBody();
var parser = new JSONParser();
var response = parser.parse(responseBody);

for (var i = 0; i < response.length; i++) {
	gs.info("TRICIA = " +response[i].risk.criticalityLabel + " name "+response[i].entity.name+ " id "+response[i].entity.id+ " score "+response[i].risk.score+ " rule "+response[i].risk.evidenceDetails[0].rule + " evidencestring "+response[i].risk.evidenceDetails[0].evidenceString);

	var parser2 = new JSONParser();
	var response2 = parser2.parse(response[i].risk.evidenceDetails);

	for(var j = 0; j < response2.length;j++){
		gs.info("TRICIA = " +response2[j].rule);
	}
}

View solution in original post

3 REPLIES 3

Mike Patel
Tera Sage

try

var responseBody = response.getBody();
var parser = new JSONParser();
var response = parser.parse(responseBody);

for (var i = 0; i < response.length; i++) {
	gs.info("TRICIA = " +response[i].risk.criticalityLabel + " name "+response[i].entity.name+ " id "+response[i].entity.id+ " score "+response[i].risk.score+ " rule "+response[i].risk.evidenceDetails[0].rule + " evidencestring "+response[i].risk.evidenceDetails[0].evidenceString);

	var parser2 = new JSONParser();
	var response2 = parser2.parse(response[i].risk.evidenceDetails);

	for(var j = 0; j < response2.length;j++){
		gs.info("TRICIA = " +response2[j].rule);
	}
}

Awesome thank you Mike!!!!! šŸ™‚

triciav
Kilo Sage

Ok thanks Mike I will try that and let you know! THank you so much šŸ™‚