简体   繁体   中英

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

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" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM