Forum Discussion

skyhigh's avatar
skyhigh
Contributor
7 years ago

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>

 

9 Replies

  • StevenColon's avatar
    StevenColon
    SmartBear Alumni (Retired)

    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. 

    • skyhigh's avatar
      skyhigh
      Contributor

      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++;
      }
      • PaulMS's avatar
        PaulMS
        Super Contributor

        Assuming that the JSON data is valid

         

        //iterate
        def int count = 0;
        parsedJson.Data.each
        {
        log.info parsedJson.Data[count].MemberName
        count++;
        }