Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Suggestions for how to assert test with embedded json in xml response

paolabs
paolabs Posts: 16

I running tests on a service that return an xml response, but some of the information is in a json format. I need to validate the json part of the test, and I don't know if there is a way of doing this without custom assertions. Has anyone done something like this before?

I need to assert for instance, that the count of elements in BookingByStatus for Status B is 4

This is an example of the response:

{"Date":"2018-12-06T00:00:00","LastBookingDateUtc":"2018-11-29T20:15:46","CalendarID":6780,"HotelID":7427,"ChainID":8001587,"BookingByStatus":[{"Count":4,"Status":"B","OnPropertyStatus":"N"},{"Count":28,"Status":"R","OnPropertyStatus":"H"},{"Count":24,"Status":"R","OnPropertyStatus":"N"},{"Count":4,"Status":"R","OnPropertyStatus":"A"},{"Count":8,"Status":"R","OnPropertyStatus":"C"},{"Count":8,"Status":"X","OnPropertyStatus":"N"}],"ArrivalByStatus":[{"Count":4,"Status":"R","OnPropertyStatus":"C"}],"DepartureByOnPropertyStatus":[{"Count":4,"Status":"B","OnPropertyStatus":"N"},{"Count":28,"Status":"R","OnPropertyStatus":"H"},{"Count":20,"Status":"R","OnPropertyStatus":"N"},{"Count":4,"Status":"R","OnPropertyStatus":"A"},{"Count":4,"Status":"R","OnPropertyStatus":"C"},{"Count":8,"Status":"X","OnPropertyStatus":"N"}],"LOSByStatus":[{"Key":3,"Count":4,"Status":"B","OnPropertyStatus":"N"},{"Key":3,"Count":28,"Status":"R","OnPropertyStatus":"H"},{"Key":3,"Count":20,"Status":"R","OnPropertyStatus":"N"},{"Key":3,"Count":4,"Status":"R","OnPropertyStatus":"A"},{"Key":3,"Count":8,"Status":"R","OnPropertyStatus":"C"},{"Key":1,"Count":4,"Status":"R","OnPropertyStatus":"N"},{"Key":3,"Count":8,"Status":"X","OnPropertyStatus":"N"}],"BookingByGuestAgeGroupAndStatus":[{"Key":"Adult","Count":4,"Status":"B","OnPropertyStatus":"N"},{"Key":"Children","Status":"B","OnPropertyStatus":"N"},{"Key":"Adult","Count":28,"Status":"R","OnPropertyStatus":"H"},{"Key":"Children","Status":"R","OnPropertyStatus":"H"},{"Key":"Adult","Count":24,"Status":"R","OnPropertyStatus":"N"},{"Key":"Children","Status":"R","OnPropertyStatus":"N"},{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"A"},{"Key":"Children","Status":"R","OnPropertyStatus":"A"},{"Key":"Adult","Count":8,"Status":"R","OnPropertyStatus":"C"},{"Key":"Children","Status":"R","OnPropertyStatus":"C"},{"Key":"Adult","Count":8,"Status":"X","OnPropertyStatus":"N"},{"Key":"Children","Status":"X","OnPropertyStatus":"N"}],"ArrivalByGuestAgeGroupAndStatus":[{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"C"},{"Key":"Children","Status":"R","OnPropertyStatus":"C"}],"DepartureByGuestAgeGroupAndStatus":[{"Key":"Adult","Count":4,"Status":"B","OnPropertyStatus":"N"},{"Key":"Children","Status":"B","OnPropertyStatus":"N"},{"Key":"Adult","Count":28,"Status":"R","OnPropertyStatus":"H"},{"Key":"Children","Status":"R","OnPropertyStatus":"H"},{"Key":"Adult","Count":20,"Status":"R","OnPropertyStatus":"N"},{"Key":"Children","Status":"R","OnPropertyStatus":"N"},{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"A"},{"Key":"Children","Status":"R","OnPropertyStatus":"A"},{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"C"},{"Key":"Children","Status":"R","OnPropertyStatus":"C"},{"Key":"Adult","Count":8,"Status":"X","OnPropertyStatus":"N"},{"Key":"Children","Status":"X","OnPropertyStatus":"N"}]}


{"Date":"2018-12-05T00:00:00","LastBookingDateUtc":"2018-11-28T21:11:59","CalendarID":6779,"HotelID":7427,"ChainID":8001587,"BookingByStatus":[{"Count":4,"Status":"B","OnPropertyStatus":"N"},{"Count":28,"Status":"R","OnPropertyStatus":"H"},{"Count":24,"Status":"R","OnPropertyStatus":"N"},{"Count":4,"Status":"R","OnPropertyStatus":"A"},{"Count":4,"Status":"R","OnPropertyStatus":"C"},{"Count":8,"Status":"X","OnPropertyStatus":"N"}],"ArrivalByStatus":[{"Count":8,"Status":"R","OnPropertyStatus":"N"}],"DepartureByOnPropertyStatus":[],"LOSByStatus":[{"Key":3,"Count":4,"Status":"B","OnPropertyStatus":"N"},{"Key":3,"Count":28,"Status":"R","OnPropertyStatus":"H"},{"Key":3,"Count":20,"Status":"R","OnPropertyStatus":"N"},{"Key":3,"Count":4,"Status":"R","OnPropertyStatus":"A"},{"Key":3,"Count":4,"Status":"R","OnPropertyStatus":"C"},{"Key":1,"Count":4,"Status":"R","OnPropertyStatus":"N"},{"Key":3,"Count":8,"Status":"X","OnPropertyStatus":"N"}],"BookingByGuestAgeGroupAndStatus":[{"Key":"Adult","Count":4,"Status":"B","OnPropertyStatus":"N"},{"Key":"Children","Status":"B","OnPropertyStatus":"N"},{"Key":"Adult","Count":28,"Status":"R","OnPropertyStatus":"H"},{"Key":"Children","Status":"R","OnPropertyStatus":"H"},{"Key":"Adult","Count":24,"Status":"R","OnPropertyStatus":"N"},{"Key":"Children","Status":"R","OnPropertyStatus":"N"},{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"A"},{"Key":"Children","Status":"R","OnPropertyStatus":"A"},{"Key":"Adult","Count":4,"Status":"R","OnPropertyStatus":"C"},{"Key":"Children","Status":"R","OnPropertyStatus":"C"},{"Key":"Adult","Count":8,"Status":"X","OnPropertyStatus":"N"},{"Key":"Children","Status":"X","OnPropertyStatus":"N"}]}

Comments

  • jakubiak
    jakubiak Posts: 813 admin

    I'm assuming that the JSON is embedded within an XML element. If that is the case, you should be able to use an XML Transformer to extract the JSON, and then send that to a JSON Assertor to do the assertions.

  • paolabs
    paolabs Posts: 16

    Yes the JSON is embedded, I looked in the help for examples of XML Transformer, but couldn't find anything in detail.

  • jakubiak
    jakubiak Posts: 813 admin

    Actually, I think this is simpler with an XML Data Bank and and JSON Assertor. Let's assume you have XML like the following:

    <xml>
      <jsonElement>
      {
          "foo":"bar"
      }
      </jsonElement>
    </xml>
    

    Create an XML Data Bank to extract the content of "jsonElement". Then create a standalone JSON Assertor. In the input tab of the JSON Assertor, use the Data Source option and select the data source column name specified in the XML Data Bank.