Forum Discussion
Hello Rao,
Thanks for your reply.
I gave sample json message that I would like to read and update via groovy scripting in SoapUI pro. I am using SoapUI NG pro licensed tool.
Can you please provide me some links where mentioned to read json message, what package to be included while running etc.
Thanks,
SL
- New2API8 years agoFrequent Contributor
Satish, below script should provide you some direction. In this example, previous step is a REST Call and it uses it's response to use as a request for another REST.
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.support.types.StringToObjectMap
import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.json.JsonSlurper
import com.eviware.soapui.support.GroovyUtils
import groovy.json.JsonBuilder
//## Get previous test step name //
def currentStepInd = context.currentStepIndex
def TestStepName = testRunner.testCase.getTestStepAt(currentStepInd - 1).name //Assuming REST request is your prior step
//## Parse json response ##
def response = context.expand( '${'+TestStepName+'#Response}' ).toString()
def json = new JsonSlurper().parseText response
//##Update JSON Nodes ##//
def jsonbuilder = new JsonBuilder(json)
jsonbuilder.content.status = 'Active' // Assuming 'Status' is one of json node
//## Build json body and write to a file as string ##//
def req = jsonbuilder.toString().replaceAll(~/\\n/,"\\n").replaceAll(~/\\r/,"\\r") //escape char handler if needed
//##Update TestCase Property to contain parsed json ##//
testRunner.testCase.testSuite.setPropertyValue("JSONBODY", "${req}")in the next REST step call this JSON using - ${#TestSuite#JSONBODY}
for Excel reading you could refer to below link-
if you store your JSON in a text file then you also use above like this
//## READ canned request from a file to modify ##//
def JSON = new File("C:\Requests\JsonBody.txt").text
def json= new JsonSlurper().parseText JSONHope this helps!