Compare 2 JSON responses for duplicate property value
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Compare 2 JSON responses for duplicate property value
Hi,
I have two JSON responses that will have dynamic nodes on each request, but same structure and properties. I need to identify if a given property has no duplicate values found in both the JSON responses via groovy. Appreciate you help. Thanks
Example below. ID-424 in JSON1 & 2 are matching.
JSON1
{
"Name" : "Name1",
"Age" : "25",
"DateOfBirth" : "01012000",
"ID" : "453"
},
{
"Name" : "Name2",
"Age" : "25",
"DateOfBirth" : "01012000",
"ID" : "424"
}
JSON2
{
"Name" : "Name3",
"Age" : "21",
"DateOfBirth" : "01012000",
"ID" : "424"
},
{
"Name" : "Name4",
"Age" : "25",
"DateOfBirth" : "01012000",
"ID" : "412"
}
Solved! Go to Solution.
- Labels:
-
Assertions
-
Data-Driven Testing
-
REST
-
Scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Looks you posted the pieces from the response. Is it possible to show the full json structure instead of pieces.
Because the solution would depend on it.
It need not be exact response but hierarchy is needed.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks @nmrao I have now attached two JSON files that would provide some idea for the structure. In these two JSON files I'm looking for duplicate values for property - "ReferenceNumber". Ideally these JSON responses Im thinking will not be saved as files to be compared, but any way we can use groovy to read the responses and find the duplicate values for property "ReferenceNumber"
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- add a script assertion to both the request test steps with the below code
- below script would check the response and makes the test fail if there are duplicate values for "ReferenceNumber" property
Hoping this is what you need, reply back otherwise.
def duplicateRefs = new groovy.json.JsonSlurper().parseText(context.response).ReferenceNumber.countBy{it}.findResults { it.value > 1 ? it.key : null }
log.info "${duplicateRefs.size() > 0 ? 'Duplicate reference numbers found :' + duplicateRefs : 'No duplicate reference numbers'}"
assert [] == duplicateRefs, "Duplicate references found, ${duplicateRefs}"
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot for this @nmrao. The script will obviously help me in identifying duplicate reference numbers in individual JSON response. However, I'm looking for any "ReferenceNumber" value in JSON1 is not matching in JSON2 and vise versa. In other words none of the reference numbers in JSON1 should match with any of the reference numbers in JSON2. Sorry for the confusion.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Any "ReferenceNumber" value should not be in common across both the JSON files.
In the attached JSON files, I have property "ReferenceNumber" value in JSON1 node1 same as JSON2 node0, that needs to be flagged. Does not matter if other properties have duplicate values, but ReferenceNumber should not be duplicate at all across the JSON1 and also in JSON2.
Regards
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can park off your responses into separate properties and then compare that way. I have a script for this cos @nmrao gave it me a while back!!😄
It'll take me a day or so to dig it out, and i'll past it in if Rao dont beat me to it.
Cheers
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
