Forum Discussion

eissaeva's avatar
3 years ago

How to dynamically compare actual and expected JSON response with Groovy?

In my project, when assertion fails, we manually compare expected with actual response.

Sometimes response can include hundreds of lines and its difficult to compare manually

How to compare partial response  ( expected to actual ) using a common groovy script which can be used with any test case as a debug step and point out differences?


When we create assertions, we capture expected response. How to dynamically capture this response using groovy script (dynamically = response which can be captured for any assertion) ?

4 Replies

  • aaronpliu's avatar
    Frequent Contributor

    eissaeva I think you seems misunderstanding response.

    response would be changed refer to your request with different parameters or payload. If you just would like to validate certain node value, for sure, it is easy to check. You may know some of node or values are optional in schema, it is not mandatory to return every time.

    you always can fetch response and parse it as a json object. Any step would read it.




    • eissaeva's avatar

      I understand and thats exactly what I want to do - dynamically compare (depending on payload and parameters) and print out the differences

      I don't want to compare specific nodes. What is left to figure out for me is how to print out differences between two JSON objects or Strings just like JSON Editor does? ( 

  • nmrao's avatar
    Champion Level 3
    Very generic question.

    Usually compared with a static actual response, say master response or gold response (which was already verified and saved). This works well if it does not have any dynamic data such as dates, or ids etc.,
    Otherwise, have to ignore certain fields while comparing.

    Again there can be soap response, json response or xml response. I don't think any single general solution can be used.
    Do you have any specific data that you need to compare?
    • eissaeva's avatar

      This is JSON response.


      I already figured out how to grab actual response (will probably pass keys as arguments to my method) - but for some reason the response appears without quotation marks, ex {key: value} but I want {"key":"value"}


      def responseMess = testRunner.testCase.testSteps["testCaseName"].testRequest.response.contentAsString
      def response = new JsonSlurper().parseText(responseMess)

      def partialactualResponse =


      I am still not sure how to grab expected response. 

      In my test I have failed assertion. How to grab expected response from there and pass it as an argument to my method?

      Unless there is a better way of doing it ..

      The screenshot above is just an example. I need to figure out how to capture response for any assertion