Ask a Question

Compare 2 JSON responses for duplicate property value

SOLVED
SanK
Occasional Contributor

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

15 REPLIES 15
nmrao
Community Hero

It would easy to compare the person once we get the exact person from the array.

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.
SanK
Occasional Contributor

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"

nmrao
Community Hero

@SanK 

- 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.
SanK
Occasional Contributor

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.

nmrao
Community Hero

Do you mean, both shouldn't have anything in common?


Regards,
Rao.
SanK
Occasional Contributor

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

richie
Community Hero

Hey @SanK,

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nmrao
Community Hero

Got you, thanks for clarifying.


Regards,
Rao.
SanK
Occasional Contributor

Hi @richie thanks. I'll wait for your script that @nmrao shared with you. I will also update the title once I got this sorted if that's what you meant :). Apprecaite  both for your assistance. 

 

Regards

cancel
Showing results for 
Search instead for 
Did you mean: