krogold
4 years agoRegular Contributor
groovy - list parsing fails if it contains one element
Hello,
I often use jsonslurper to parse lists returned by test steps, but I have a case where it fails.
I have a testStep that is a call to an API, it returns a list of elements such as ["fim:device:LIGHT:1810","fim:device:LIGHT:1810", etc.]
In the next step I want to parse this list, so I use a jSonSlurper to convert it into an array doing
def light_list = context.expand( '${get all light devices#testResponse#$[\'result\']}' )
log.info light_list // ["fim:device:LIGHT:1810","fim:device:LIGHT:1811"]
log.info light_list.getClass() //=> string
def jsonSlurper = new groovy.json.JsonSlurper()
light_array = jsonSlurper.parseText(light_list)
light_array.each{
log.info it // => lists my two items
}
However I encounter a failure if that list contains only one element !
in that case
log.info light_list // [fim:device:LIGHT:1810]
which leads to a failure when I try to parse it with the jSonSlurper ...
the response from the previous step is a String, in both cases,
How can I ensure that the 'format' does not change ?
- It appars this and below thread are related
https://community.smartbear.com/t5/API-Functional-Security-Testing/decoding-a-string-that-contains-a-json-that-contains-an-array/m-p/216927#M48839
If so, can this be closed as well?