0

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

1 Answer 1

2

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" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.