Nisha Nisha - 8 months ago 141
reST (reStructuredText) Question

groovy script SOAP UI json response parse

How to parse

json
response using groovy script.

I am using SOAP UI and have json response as below-

{
"resource": {
"name":"aaaaaaaaaaa",
"emailid":"bbbbbbbbb"
}
}


Can anyone please share sample code to parse json object and post that some basic assertions check.
Thanks

Rao Rao
Answer Source

Add a Script Assertion for the rest request test step with below script.

  • Define your expected data as shown in the snippet below as needed
  • It compares each key value with expected data.

JsonSlurper can be used to parse the response.

//Check if the response is not empty
assert context.response, 'Response is empty or null'

//Define expected data
def expectedData = [name: 'aaaaaaaaaaa', emailid: 'bbbbbbbbb']

def json = new groovy.json.JsonSlurper().parseText(context.response)
//Checks all elements of resource one by one and compare with expectedData
json.resource.each {k, v -> assert v == expectedData."$k" }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download