Ask a Question

groovy - list parsing fails if it contains one element

SOLVED
krogold
Regular 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 ?

 

 

 

9 REPLIES 9
nmrao
Community Hero

The response does not seem to be a valid json or json array. Isn't it?

["fim:device:LIGHT:1810","fim:device:LIGHT:1810", etc.]

 



Regards,
Rao.
krogold
Regular Contributor

it's just for the example, you can see it better in the code example

nmrao
Community Hero

Are you referring to
["fim:device:LIGHT:1810","fim:device:LIGHT:1811"] => this is not json or json array.
Can you please provide the sample response ?


Regards,
Rao.
krogold
Regular Contributor

The original response is a json array from which I extract a parameter "result" that contains an array of strings.

Indeed "result" is not a json array ... I should then use another way to extract data, such as tokenize, I guess ?

**bleep**. jSonSlurper makes it, usually ...

 

 

nmrao
Community Hero

Yes, if you know the extracted data format and which value that is needed.


Regards,
Rao.
krogold
Regular Contributor

Hello,

I have further questions about this. If my list of element contains json objects that also contain commas, if I do tokenize, I will end up stplitting all of it. Is there a way to tokenize a string but only on particular separators, ie.

I have a string "[{object1},{object2},...,{objectn}]"

if objects contain ',' which is usually the case for json objects, how can I do to obtain this string in the form of a collection so that when I do myList.each{println it} I have

{object1}

{object2}

...

{objectn}

as a result ?

 

nmrao
Community Hero

Sample data of {object} please?

In case if the each object looks like as shown in the picture, then you can read as shown there

 

nmrao_0-1620133902804.png

 



Regards,
Rao.
nmrao
Community Hero

It appars this and below thread are related
https://community.smartbear.com/t5/API-Functional-Security-Testing/decoding-a-string-that-contains-a...

If so, can this be closed as well?


Regards,
Rao.
krogold
Regular Contributor

Yes,they are linked.

Thank you

cancel
Showing results for 
Search instead for 
Did you mean: