Forum Discussion

SanK's avatar
SanK
Occasional Contributor
5 years ago
Solved

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

  • Check your auto-wait setting:

     

    https://support.smartbear.com/testcomplete/docs/app-objects/common-tasks/waiting-process-or-window-activation.html

     

    Using the Default Auto-Wait Timeout

    By default, TestComplete is waiting for objects, processes and windows for a period of time. This time period is specified by the Auto-wait timeout project option. By default, it is 10 seconds. If an object, window or process has not been found by the end of the specified period, the test fails.

    The Auto-wait timeout affects the following methods:

  • Hi,

     

    from TC12.50 to TC14.50 [...] during testrun the tc-indicator shows wait for object

    There were a lot of performance optimizations between these versions, so, most probably, your test code now executes faster and waits for the object where previously object had time to appear before execution reached this point.

    I would say that it is OK and nothing wrong as long as you don't see any problems reported to test log.

     

  • -> Using the Default Auto-Wait Timeout

    hi Marsha,

    the default timeout setting is not the point in my case, the problem is that objects are found and verified in keywordtests and it shows no error in testlog, but somehow certain objects still run into timeout and delay the test run.

    Lee

     

15 Replies

  • nmrao's avatar
    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.
    • SanK's avatar
      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's avatar
        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}"