Ask a Question

Sort REST request response array

skelander
Occasional Contributor

Sort REST request response array

Hello!

 

I wonder if there is a way to sort the (json) response from a REST Request teststep?

 

Thanks,

Rikard

2 REPLIES 2
StevenColon
Staff

Thank you for posting to our Community Forum.

 

There is currently no way to sort a JSON response through the REST Request teststep.

 

The best way to do this currently is to write a script in a groovy script test step after the request and use JSlurper to sort the information.

 

Here is an example script (created in ReadyAPI 2.1) that gets the response data, parses it with JSlurper, and sorts it.

 

def response = context.expand( '${GET Request#Response}' ) 
//Property Expansion Format: '${<TESTSTEP>#Response}' def slurper = new groovy.json.JsonSlurper() def result = slurper.parseText(response) //Prints out JSON sorted alphabetically by key log.info result.sort()


You can then put that value into a property so that other test steps can use it. Here is the code line to do that:

 

testRunner.getTestCase().setPropertyValue("sortedJSON", result.toString())

Let me know if you have any questions/concerns. 

Best,
Steven
Atlassian Ecosystem Manager
nmrao
Community Hero

Can you explain the question with a sample data?


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: