Forum Discussion

Shehnaz's avatar
Shehnaz
Occasional Contributor
4 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.