skyhigh
8 years agoContributor
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"> <Data> <e> <MemberAge>31</MemberAge> <MemberGender>Femalse</MemberGender> <MemberName>Erika Garcia</MemberName> </e> <e> <MemberAge>25</MemberAge> <MemberGender>Male</MemberGender> <MemberName>James Nelson</MemberName> </e> </Data> <IsRedirect>false</IsRedirect> <IsSuccess>true</IsSuccess> <RedirectUrl/> </Response>
Assuming that the JSON data is valid
//iterate
def int count = 0;
parsedJson.Data.each
{
log.info parsedJson.Data[count].MemberName
count++;
}Here is the script assertion for json.
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/json/JsonArraySample.groovy
You can also try the online demo for the same:
https://ideone.com/Z3wyfh