'How to compare and get differences of responses in Groovy?

How to compare and get differences of responses in Groovy?

RESPONSE_1 OUTPUT

{"entity":[{"employer1":[{"name":"John","age": 23}]}]}]}

RESPONSE_2 OUTPUT

{"entity":[{"employer1":[{"name":"John","age": 23}]}],"employer2":[{"name": "Mike","age": 26}]}]}

This one is my code. And it res1Differnce only returns null

stage('comparison') {
steps {
  script {
   if (RESPONSE_1 == RESPONSE_2) {
    println("Matched")
   } else {
    println("Does not match")
    def res1Difference = RESPONSE_1.minus(RESPONSE_2)
    def res2Difference = RESPONSE_2.minus(RESPONSE_1)
    echo "${res1Difference}"
    echo "${res2Difference}"
   }
  }
}
}

I also tried to parse the RESPONSE_1 and RESPONSE_2 to map then used the .minus() and .remove() and .removeAll(). If .minus() or .removeAll() it returns hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature method: .... If .remove() it returns null.

def res1 = readJSON text: RESPONSE_1
def res2 = readJSON text: RESPONSE_2 

res1.each { entry ->
def res2Difference = res2.minus("$entry")
}
echo "${res2Difference}"

I also tried this one, same output as the other

def res1 = res1.remove{entry -> 
res2.contains("$entry.key:$entry.value")
}
echo "${res1Difference}"

Pls help me out how to return the differences of both responses.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source