Ask a Question

How to parse JSON or XML in Assertions


How to parse JSON or XML in Assertions

Hi All,


I'm trying to parse each response value from a response format either XML or JSON.  I found some posts and tried them, but couldn't get it work because I always get errors.  Can someone show me how to parse the response below in the Assertions.  I got this format reponse from the Reponse window in SoapUI.


This is what JSON response looks like:

   "IsSuccess": true,
   "Data":    [
         "MemberAge": 31,
         "MemberGender": "Female",
         "MemberName": "Erika Garcia",
         "MemberAge": 25,
         "MemberGender": "Male",
         "MemberName": "James Nelson",
   "IsRedirect": false,
   "RedirectUrl": ""

This is what XML response looks like:

<Response xmlns="https://<domain name>/Member/Account">
         <MemberName>Erika Garcia</MemberName>
         <MemberName>James Nelson</MemberName>


SmartBear Alumni (Retired)

Thank you for posting to our Community Forum. 


Here is our documentation on JSONPath and XPath match Assertions.


ReadyAPI will create these assertions based on a response automatically if  you right-click a node under the outline tab for the response. 



 Let me know if you have any questions/concerns. 

Atlassian Ecosystem Manager

Wow that is very slick.  I didn't know we could do right-click to generate the assertion.  Thanks!


However I would like to learn how to use Assertions script, so can you look at the script below of what's wrong with it because it does not print out all the MemberName.  I have 20 members, but it shows only 5 members printing to the output screen.


//grab the response
def responseMsg = messageExchange.response.responseContent
def parsedJson = new groovy.json.JsonSlurper().parseText(responseMsg)

//get IsSuccess parsedJson.IsSuccess

def int count = 0;
{ parsedJson.Data[count].MemberName

//also try loop, but it does not print all members
def int count = 0;
for(content in parsedJson)
{ parsedJson.Data[count].MemberName;

When you use JsonSlurper().parseText(), I was under the impression that it parses text! "Parse a text representation of a JSON data structure" So it bothers me you have false and true without quotes. 


I would access isSuccess as follows


import groovy.json.JsonSlurper

def responseMessage = messageExchange.response.responseContent

def parsedJson = new JsonSlurper().parseText(responseMessage)

assert parsedJson.isSuccess == "true"


Take a peek at this


I am not sure how I would address the issue with the loop.


But your loop does not seem to end in that snippet of code. Could that be causing a problem? Why don't you try limiting it to 6? Then try a higher limit on the variable count?



Super Contributor

Assuming that the JSON data is valid


def int count = 0;
{ parsedJson.Data[count].MemberName

Champion Level 3


Here is the script assertion for json.

You can also try the online demo for the same:

Champion Level 3

Thank you to both nmarao and PaulMS.  Both suggestions work for me.

Numbers, Boolean's, and Null are not considered to be strings and therefor do not use "" double quotes.


Old Dog New trick 🙂

Numbers, Boolean's, and Null are not considered to be strings and therefor do not use "" double quotes.

Showing results for 
Search instead for 
Did you mean: