cancel
Showing results for 
Search instead for 
Did you mean: 

Compare two REST response and ignore some fields

SOLVED
Highlighted
Occasional Contributor

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
}

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: Compare two REST response and ignore some fields

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
}

View solution in original post

7 REPLIES 7
Highlighted
Community Hero

Re: Compare two REST response and ignore some fields

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.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Occasional Contributor

Re: Compare two REST response and ignore some fields

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",

}

Highlighted
Community Hero

Re: Compare two REST response and ignore some fields

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.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Occasional Contributor

Re: Compare two REST response and ignore some fields

do you have some list of jar that can be used in the current context

Highlighted
Community Hero

Re: Compare two REST response and ignore some fields

You can find one called "JSONAssert" and "JSONUnit" but I was not able to get either of them to work with groovy scripting. Whether my own lack of knowledge or not being able to lean on fully fledged java code in groovy got in my way, I am not sure. But I fiddled with both for a few hours today and was not able to get either to work.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Community Leader

Re: Compare two REST response and ignore some fields

@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.

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Occasional Contributor

Re: Compare two REST response and ignore some fields

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
}

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors