Forum Discussion

skelander's avatar
skelander
Occasional Contributor
7 years ago

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

  • StevenColon's avatar
    StevenColon
    SmartBear Alumni (Retired)

    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. 

  • nmrao's avatar
    nmrao
    Champion Level 3
    Can you explain the question with a sample data?