Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

Assert Repeating JSON Attribute Sorted Order

Hi,

 

A while ago I asked for help on asserting a repeating attribute's value in a json response was sorted in ascending and descending and you guys gave me an answer - but it's problematic because the solution requires me to list the possible values in an array to assert the sort order is correct, however I have thousands of records for some of my responses and so this approach won't work.

 

I've been searching and found the following post that groovyguy and nmrao provided answers to handle this  - unfortunately I can't quite read the code so can't tailor it to my problem - the original code is asserting that XML tag's id attribute values are sorted in ascending and descending order as follows:

def xml = """<?xml version="1.0" encoding="UTF-8"?> <er-response>    <subscriptions-v2>          <subscription id="909" status="1" />          <subscription id="908" status="1" />          <subscription id="904" status="2" />          <subscription id="898" status="1" />          <subscription id="897" status="1" />          <subscription id="896" status="2" />          <subscription id="895" status="1" />          <subscription id="7" status="1" />          <subscription id="6" status="1" />          <subscription id="5" status="11" />       </subscriptions-v2>   </er-response>"""
def parsedXml = new XmlSlurper().parseText(xml)
//Get the ids from the response
def idsFromResponse = parsedXml.'**'.findAll{ it.name() == 'subscription'}.collect{it.@id.text()}
println "Original list from response : $idsFromResponse"
//Sort the numbers in descending order
def descIds = idsFromResponse.sort(false).reverse()
assert idsFromResponse == descIds, "Response does not have the ids in descending order"

I was trying to edit it so that  it supports the asserting on the repeating 'Name' attribute within my json - the Name attributes JSONPATH is as follows: $['data'][x]['Name'] (I will attach an example response)

 

def response = context.expand( '${REST Request#Response}' ) 
def json = new groovy.json.JsonSlurper().parseText(response)

def idsFromResponse = json.'**'.findAll{ it.name() == 'Name'}.collect{it.@id.text()} //<<< this is the line I'm having trouble with - I believe I need to alter this to point to the 'Name' attribute in my response
println "Original list from response : $idsFromResponse"
//Sort the numbers in descending order
def descIds = idsFromResponse.sort(false).reverse()
assert idsFromResponse == descIds, "Response does not have the ids in descending order"

Can anyone provide any assistance how to alter the relevant line to point to the 'Name' attribute, JSONPATH=$['data'][x]['Name']??

 

Thanks to all,

 

richie

 

 

 

  • You can try replacing your idsFromResponse line with this one below. That being said, the sort() method appears to have issues with some of the symbols in the names you are sorting. It actually appears to put the list out of order and makes the script fail. Not sure if you're account for that or not. 

     

    def idsFromResponse = json.data.Name;

     

4 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    You can try replacing your idsFromResponse line with this one below. That being said, the sort() method appears to have issues with some of the symbols in the names you are sorting. It actually appears to put the list out of order and makes the script fail. Not sure if you're account for that or not. 

     

    def idsFromResponse = json.data.Name;

     

      • richie's avatar
        richie
        Community Hero

        absolutely!

         

        msiadak totally sorted me out!

         

        Cheers fella,

         

        richie