groovy - list parsing fails if it contains one element
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
it's just for the example, you can see it better in the code example
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
["fim:device:LIGHT:1810","fim:device:LIGHT:1811"] => this is not json or json array.
Can you please provide the sample response ?
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes,they are linked.
Thank you
