How to parse JSON or XML in Assertions
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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>
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for posting to our Community Forum.
Here is our documentation on JSONPath and XPath match Assertions.
https://support.smartbear.com/readyapi/docs/testing/assertions/reference/property/json-match.html
https://support.smartbear.com/readyapi/docs/testing/assertions/reference/property/xpath-match.html
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.
Steven
Atlassian Ecosystem Manager
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 log.info parsedJson.IsSuccess //iterate def int count = 0; parsedJson.each { log.info parsedJson.Data[count].MemberName count++; } //also try loop, but it does not print all members def int count = 0; for(content in parsedJson) { log.info parsedJson.Data[count].MemberName; count++; }
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html
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?
Bill
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Assuming that the JSON data is valid
//iterate
def int count = 0;
parsedJson.Data.each
{
log.info parsedJson.Data[count].MemberName
count++;
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/xml/XmlWithArrayOfElements.groovy
Online demo: https://ideone.com/FGSawS
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you to both nmarao and PaulMS. Both suggestions work for me.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Numbers, Boolean's, and Null are not considered to be strings and therefor do not use "" double quotes.
https://www.w3schools.com/js/js_json_datatypes.asp
Old Dog New trick 🙂
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
https://www.welookups.com/js/js_json.html
Numbers, Boolean's, and Null are not considered to be strings and therefor do not use "" double quotes.
