Solved
Forum Discussion
groovyguy
9 years agoCommunity Hero
Here's what I have. It'll need some customizing based on where your json response is.
package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
// get the response of the JSON test step / request.
// def response = context.expand( '${TestStepName s#Response}' ).toString()
// below is a string representing the value provided in the forum post. The above line can replace the below, with appropriate name of JSON response location
def response = "[{\n\"trackingEntryId\": 24,\n\"documentId\": 24,\n\"fileId\": 24,\n\"fileName\": \"Sample.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Gibson, Mike\",\n\"uploadedDate\": \"2017-06-21T08:31:23.2021627\",\n\"transactions\": 1\n},\n{\n\"trackingEntryId\": 25,\n\"documentId\": 25,\n\"fileId\": 25,\n\"fileName\": \"Sample1.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Hill, Lucy\",\n\"uploadedDate\": \"2017-06-21T08:32:05.4842927\",\n\"transactions\": 1\n},\n{\n\"trackingEntryId\": 26,\n\"documentId\": 26,\n\"fileId\": 26,\n\"fileName\": \"TrackIT_Test4.xlsx\",\n\"status\": \"Success\",\n\"uploadedBy\": \"Sinatra, Frank\",\n\"uploadedDate\": \"2017-06-21T08:33:24.0482512\",\n\"transactions\": 0\n}]";
// Set up a JsonSlurper and parse the response
def json = new JsonSlurper().parseText(response);
// This tests that the collection of "uploadedBy", when sorted, is equal to the original list.
// Setting sort to false returns a copy of the list without sorting the original.
log.info((json.uploadedBy.sort(false) == json.uploadedBy));
assert (json.uploadedBy.sort(false) == json.uploadedBy);
// To assert that the list is in descending order, you can use the same logic and the reverse of the list.
// assert (json.uploadedBy.sort(false) == json.uploadedBy.reverse());