Forum Discussion

Shehnaz's avatar
Shehnaz
Occasional Contributor
3 years ago
Solved

Sorting data in JSON response for comparison

I am trying to compare 2 full JSON responses - expected and actual for Pass/Fail status. After running the REST request, I get Actual response that does not return the field in same sequence as in Expected Response which causes 1 on 1 comparison to fail. To solve this issue, I need to sort the data in Actual Response to match it to Expected Response format before comparison. Below is the Expected and Actual response for reference. Can anyone help with this?

 

Expected Response -

{
"Drivers": [
{
"ContactId": 797952,
"LicenseNumber": "123WT45TY1234",
"LicenseState": "DE",
"DriverNumber": 2,
"DriverStatus": "Active",
"DriverTerminationDate": null,
"DriverUsed": true,
"PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
"PolicyDateTimeMax": "2021-01-06T14:27:34"
},
{
"ContactId": 797949,
"LicenseNumber": "N677DG8906VB7",
"LicenseState": "DE",
"DriverNumber": 1,
"DriverStatus": "Active",
"DriverTerminationDate": null,
"DriverUsed": true,
"PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
"PolicyDateTimeMax": "2021-01-06T14:27:34"
}
]

 

Actual Response - 

{
"Drivers": [
{
"ContactId": 797949,
"LicenseNumber": "N677DG8906VB7",
"LicenseState": "DE",
"DriverNumber": 1,
"DriverStatus": "Active",
"DriverTerminationDate": null,
"DriverUsed": true,
"PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
"PolicyDateTimeMax": "2021-01-06T14:27:34"
},
{
"ContactId": 797952,
"LicenseNumber": "123WT45TY1234",
"LicenseState": "DE",
"DriverNumber": 2,
"DriverStatus": "Active",
"DriverTerminationDate": null,
"DriverUsed": true,
"PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
"PolicyDateTimeMax": "2021-01-06T14:27:34"
}
]

 

I have highlighted field DriverNumber that I would like to sort the data before comparison in descending order 2,1,0.

 

  • Hello Shehnaz 

    Below are two code examples of groovy scripts that can compare JSON... 

     

    One:  It just sorts whatever the comparison strings are (regardless of JSON or not). Since your example is basically the same except for being out of order, you could use this example with your JSON.  It will sort each content (actual and expected) and compare them and show the difference or equality in the log output.

     

    Two: Use the JSONAssert library (Obtain "jsonassert-1.5.0.jar" from http://jsonassert.skyscreamer.org/). You will need to put the .jar file in you installation .lib folder and then restart SoapUI/ReadyAPI.  Provide the library method with your actual and expected and it will respond with differences or equality.  The LENIENT parameter is used in this case since the order is different, but textual differences will be identified.  If you use STRICT, it will not allow a different order.

     

    In either of these examples change a value and see what kind of response is returned when the comparison strings contain a different element value.

    Maybe one of these examples will meet your need.

     

    Regards,

    Todd

     

    Example One:  Groovy Script Sort And Compare Strings

    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    def refStr = """
    {
      "Drivers": [
        {
          "ContactId": 797952,
          "LicenseNumber": "123WT45TY1234",
          "LicenseState": "DE",
          "DriverNumber": 2,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        },
        {
          "ContactId": 797949,
          "LicenseNumber": "N677DG8906VB7",
          "LicenseState": "DE",
          "DriverNumber": 1,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        }
      ]
    }
    """;
    refStr = refStr.replaceAll('}', '');   // without this, there is a close brace whose result is only differentiated by its existance
    def refStrList = refStr.split();
    def refStrListSorted = refStrList.sort();
    
    log.info "refStrList.size()=" + refStrList.size();
    refStrList.each { element ->
       log.info "element=" + element;
    };
    log.info "";
    log.info "";
    refStrListSorted.each { element ->
       log.info "element=" + element;
    };
    
    
    log.info "";
    log.info "==================================================";
    log.info "";
    
    def compStr = """
    {
      "Drivers": [
        {
          "ContactId": 797949,
          "LicenseNumber": "N677DG8906VB7",
          "LicenseState": "DE",
          "DriverNumber": 1,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        },
        {
          "ContactId": 797952,
          "LicenseNumber": "123WT45TY1234",
          "LicenseState": "DE",
          "DriverNumber": 2,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        }
      ]
    }
    """;
    compStr = compStr.replaceAll('}', '');  // without this, there is a close brace whose result is only differentiated by its existance
    def compStrList = compStr.split();
    def compStrListSorted = compStrList.sort();
    
    log.info "compStrList.size()=" + compStrList.size();
    compStrList.each { element ->
       log.info "element=" + element;
    };
    log.info "";
    log.info "";
    compStrListSorted.each { element ->
       log.info "element=" + element;
    };
    
    
    log.info "";
    log.info "==================================================";
    log.info "";
    
    // sanity check...
    log.info "refStrList.size()=" + refStrList.size();
    log.info "compStrList.size()=" + compStrList.size();
    assert refStrList.size().equals(compStrList.size()), "Mismatch - There are ${refStrList.size()} reference values and ${compStrList.size()} compare values";
    
    // sorted line by sorted line check...
    refStrListSorted.eachWithIndex { refValue, idx ->
       def compValue = compStrListSorted[idx];
       if ( refValue != compValue ) {
          log.info "refValue=" + refValue + "  " + idx;
          log.info "compValue=" + compValue + "  " + idx;
          assert refValue.equals(compValue), "Mismatch - At index $idx the referece value '$refValue' does not match the comparison value '$compValue'";
       };
    };
    
    // If we get here, then the contents are equal
    log.info "";
    log.info "   ***  Reference and Compare strings are equal  ***";
    
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

     

    Example Two: Groovy Script JSON Assert Lenient

    import org.skyscreamer.jsonassert.*;      // Obtain jsonassert-1.5.0.jar from http://jsonassert.skyscreamer.org/
    
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    def String refStr = """
    {
      "Drivers": [
        {
          "ContactId": 797952,
          "LicenseNumber": "123WT45TY1234",
          "LicenseState": "DE",
          "DriverNumber": 2,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        },
        {
          "ContactId": 797949,
          "LicenseNumber": "N677DG8906VB7",
          "LicenseState": "DE",
          "DriverNumber": 1,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        }
      ]
    }
    """;
    
    //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    def String compStr = """
    {
      "Drivers": [
        {
          "ContactId": 797949,
          "LicenseNumber": "N677DG8906VB7",
          "LicenseState": "DE",
          "DriverNumber": 1,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        },
        {
          "ContactId": 797952,
          "LicenseNumber": "123WT45TY1234",
          "LicenseState": "DE",
          "DriverNumber": 2,
          "DriverStatus": "Active",
          "DriverTerminationDate": null,
          "DriverUsed": true,
          "PolicyDateTimeDriverAdd": "2021-01-06T14:27:34",
          "PolicyDateTimeMax": "2021-01-06T14:27:34"
        }
      ]
    }
    """;
    
    log.info "";
    log.info "==================================================";
    log.info "";
    
    def actual = refStr;
    def expected = compStr;
    
    //JSONAssert.assertEquals(actual, expected, JSONCompareMode.STRICT);
    JSONAssert.assertEquals(actual, expected, JSONCompareMode.LENIENT);
    
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

     

     

5 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Take the respective driver from expected and match with the respective actual.
    However, based on the above response, please clarify the following
    - Is DriverNumber always unique? Looks it does not seem to be.