Howto: Use JSON String as a Data Source in SoapUI

So, recently I had to create a test case in SoapUI that would be calling a single endpoint multiple times. The only difference between each execution is the value of an argument that is being passed in. The caveat was that I had a to call another endpoint which would give me a JSON with values that needed to be checked. I figured that the best way would be get JSON response from the first test step and create a DataSource Groovy step that would parse that JSON and iterate over the results. So, here is my DataSource Groovy step that made it all possible:

import groovy.json.JsonSlurper
 
row = testRunner.testCase.testSteps["DataSource"].currentRow
def testCase = testRunner.testCase.getTestStepByName("get json data");
def responseJson = testCase.getContentAsString();
 
def json = new JsonSlurper().parseText(responseJson);
def count = json.ids.size()
 
if ((count == null) || (count == 0)) {
	count = 1;
}
 
if (row < count) {
	def item = json.ids[row]
 
	result["id"] = item["Series Name"]
}

Oh, in order for this to work, you must add a property to the DataSource and name it 'id'.

Comments