Forum Discussion

vpnkumar's avatar
vpnkumar
Occasional Contributor
5 years ago

Compare two REST response and ignore some fields

to compare two json response i can use below function but in the current context response return some dynamic fields which will be unique every time, can some one please explain how we can handle this

 

void compareResponse(def atcualresponse, def expectedREsponse){
def map1 = new JsonSlurper().parseText(atcualresponse)
def map2 = new JsonSlurper().parseText(expectedREsponse)

assert map1 == map2
}

  • vpnkumar's avatar
    vpnkumar
    5 years ago

    Thanks Msiadak and HimanshuTayal, I was able to solve it at last  below is the generic function that I used and solved most of my needs 

    private void compare(def expected, def actual, def ignore)
    {
    def expectedParse = new JsonSlurper().parseText(expected)
    def actualParse = new JsonSlurper().parseText(actual)
    def parentnode
    def dynmaicparent
    def ignorefields = ignore.split(',')
    for(int i =0; i <ignorefields.size(); i++){
    values=ignorefields[i].trim()
    //log.info( i + "id to remove " + values)
    if(values.contains('.')){
    parentnode = values.tokenize(".")[0]
    childnode = values.substring(parentnode.length()+1)
    log.info( expectedParse.get(parentnode).size())
    for(int j =0; j <expectedParse.get(parentnode).size(); j++){
    expectedParse.get(parentnode).get(j).remove(childnode)
    actualParse.get(parentnode).get(j).remove(childnode)
    }
    }
    else
    {
    expectedParse.remove(values)
    actualParse.remove(values)
    }
    }
    log.info("after removing ignore field $expectedParse")
    log.info("updated actualParse is $actualParse")
    assert expectedParse == actualParse
    }

  • vpnkumar :

     

    As you written some values are dynamically changing, so is it changing for 1 json and not changing for another json?

     

    if this is the scenario then you need to write your custom code to match two json by ignoring those dynamic fields.

     

    • vpnkumar's avatar
      vpnkumar
      Occasional Contributor

      Thanks Msiadak and HimanshuTayal, I was able to solve it at last  below is the generic function that I used and solved most of my needs 

      private void compare(def expected, def actual, def ignore)
      {
      def expectedParse = new JsonSlurper().parseText(expected)
      def actualParse = new JsonSlurper().parseText(actual)
      def parentnode
      def dynmaicparent
      def ignorefields = ignore.split(',')
      for(int i =0; i <ignorefields.size(); i++){
      values=ignorefields[i].trim()
      //log.info( i + "id to remove " + values)
      if(values.contains('.')){
      parentnode = values.tokenize(".")[0]
      childnode = values.substring(parentnode.length()+1)
      log.info( expectedParse.get(parentnode).size())
      for(int j =0; j <expectedParse.get(parentnode).size(); j++){
      expectedParse.get(parentnode).get(j).remove(childnode)
      actualParse.get(parentnode).get(j).remove(childnode)
      }
      }
      else
      {
      expectedParse.remove(values)
      actualParse.remove(values)
      }
      }
      log.info("after removing ignore field $expectedParse")
      log.info("updated actualParse is $actualParse")
      assert expectedParse == actualParse
      }

  • groovyguy's avatar
    groovyguy
    Community Hero

    There's no easy way to do this that I am aware of. I am still learning a lot of JSON stuff myself so I may be missing something. Even so, I do not think there's a straight forward way without you building or providing some logic to force the groovy script to ignore said dynamic fields.

    That being said, if you can provide the json responses that would help. Indicate also which fields you need ignored.

    • vpnkumar's avatar
      vpnkumar
      Occasional Contributor

      This is the sample request, where all the nodes, sub nodes and their respective values need to be compared but VId, qn, LId and SessionId are the nodes which should be ignored

      {
      "Sample1": [
      {
      "deatils1": [
      {
      "Key": "value"
      }
      ],
      "details2": {
      "d1": "v1",
      "d2": "v2",
      "d3": "v3",
      "d4": "v4",
      "d5": "v5",
      "d6": "v6"
      },
      "VId": "65465456465465465hgjhffjlk",
      }
      ],
      "Status": "good",
      "qn": "BX10149384",
      "LId": "45675675675465465",
      "SessionId": "656465456435353543",

      }

      • groovyguy's avatar
        groovyguy
        Community Hero

        I did some research into this and I do not see it being possible without including extra JAR files to introduce the required behavior. At least, not easily, and outside of my experience with JSON objects. Sorry. I may keep digging in but this will require some heavy lifting and custom code, which may not be ideal if upkeep of said code may be a problem.