cancel
Showing results for 
Search instead for 
Did you mean: 

How to validate whether an array of objects in the response is sorted by a field ??

SOLVED
Highlighted
Occasional Contributor

How to validate whether an array of objects in the response is sorted by a field ??

Sample Response

{
"result": {
"list": [
{
"proceduresId": 282,
"name": "dark knightnew batch workflow 123",
"templateId": null,
"isLocked": 0
},
{
"proceduresId": 281,
"name": "latest_trendsnew batch workflow 123",
"templateId": null,
"isLocked": 0
},
{
"proceduresId": 284,
"name": "new batch workflow 123",
"templateId": null,
"isLocked": 0
},
{
"proceduresId": 280,
"name": "new batch workflow 123new batch workflow 123",
"templateId": null,
"isLocked": 0
}
],
"count": 4
},


Hi,
I am trying to write a script assertion to validate whether the objects in list array are sorted by "proceduresId" field. Please help.

 

Thanks
Harsha

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

This script will work for the sample provided and can be easily adapted.

 


// get test response as json
def json = new groovy.json.JsonSlurper().parseText(context.response)
// build array of the result list, specifically the proceduresId element
def array = json.result.list.proceduresId;

// a quick test if an array is sorted is to compare it to its sorted self. 
// FALSE means make a copy of the array and sort it, instead of sorting the original object
assert (array == array.sort(false));



---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

2 REPLIES 2
Highlighted
Community Hero

This script will work for the sample provided and can be easily adapted.

 


// get test response as json
def json = new groovy.json.JsonSlurper().parseText(context.response)
// build array of the result list, specifically the proceduresId element
def array = json.result.list.proceduresId;

// a quick test if an array is sorted is to compare it to its sorted self. 
// FALSE means make a copy of the array and sort it, instead of sorting the original object
assert (array == array.sort(false));



---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Highlighted
Occasional Contributor

Thanks a lot @msiadak ..it's working 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors