Convert String variable to a List [Groovy]
76,715
Solution 1
def l = Eval.me(ids)
Takes the string of groovy code (in this case "[10,1,9]") and evaluates it as groovy. This will give you a list of 3 ints.
Solution 2
def l = ids.split(',').collect{it as int}
Solution 3
Use the built-in JsonSlurper!
Using Eval
is dangerous and the string manipulation solution will fail once the data type is changed so it is not adaptable. So it's best to use JsonSlurper.
import groovy.json.JsonSlurper
//List of ints
def ids = "[10, 1, 9]"
def idList = new JsonSlurper().parseText(ids)
assert 10 == idList[0]
//List of strings
def ids = '["10", "1", "9"]'
idList = new JsonSlurper().parseText(ids)
assert '10' == idList[0]
Solution 4
This does work for me. And Eval.me
will not work in Jenkins groovy script. I have tried.
assert "[a,b,c]".tokenize(',[]') == [a,b,c]
Related videos on Youtube
Author by
user2068981
Updated on July 09, 2022Comments
-
user2068981 almost 2 years
How can I convert this
String
variable to aList
?def ids = "[10, 1, 9]"
I tried with: as
List
andtoList();
-
Admin about 11 yearsIt is already a list in groovy?
-
Reimeus about 11 yearsThis is a
List
do you want to convert it to aString
? -
user2068981 about 11 yearsBut when I do ids.each{println it} I have this result : [</br> 1</br> 0</br> ,</br> </br> 1</br> ]</br>
-
-
Sergio Martinez about 11 yearsI think you want to make a string "10,1,9" into a list [10,1,9]
-
user2068981 about 11 yearsdef id = ids.substring(1,ids.length()-1) def l= id.split(',').collect{it as int}
-
user2068981 about 11 yearsI find this solution but I don't think is the best : def id = ids.substring(1,ids.length()-1) def l= id.split(',').collect{it as int}
-
Alexander Suraphel about 7 years
-
chrylis -cautiouslyoptimistic- over 5 years@AlexanderSuraphel Wrong language.
-
Alexander Suraphel over 5 years@chrylis Meaning?
-
Michael A. over 5 years@AlexanderSuraphel I will answer for him: your article is related to JavaScript (JSLint specifically). But question itself was about Groovy.
-
Alexander Suraphel over 5 years@MichaelA. Have you read the linked articles? The problem is not specific to JavaScript's Eval. It's the idea of passing a string to a method that is willing to execute anything. I felt that OP needs to know the risks as well.
-
Alexander Suraphel over 5 yearsI think a better link is stackoverflow.com/questions/86513/…. The ideas discussed apply to any language, not just JavaScript.
-
CIsForCookies over 4 years
Eval.me
didn't work for me as well (Jenkins groovy script). However, your solution gave a wrong result. My str is "['/a/b/c/d@2/e/f/g/h/i/j/k/l.py::m[n-10-3-9-0/8-17-12]',]" (actually I have more entries in the string, but this is the general idea). The tokenize returns only the inner list, i.e. "[n-10-3-9-0/8-17-12]'" -
Vladimir over 4 yearsThe higher scoring answers didn't work for me. This one did.
-
Pavol Travnik about 4 yearsI would not consider using Eval as a safe option though.
-
Pavol Travnik about 4 yearsThis should definitely be accepted as right answer.
-
Max Cascone over 3 yearsthis works great for me, specifically in Jenkins, and using String instead of Int:
files = "'f1','f2'" list = files.split(',').collect{it as String} > list==['f1', 'f2'] > list[1]=='f2'
-
Max Cascone over 3 yearsupdating mine after the edit window closed:
list = versionFile.split(',').collect()
works fine for strings. -
herm over 2 yearsFor me with strings this only works when I have : '["string1", "string2"]' and not with "['string1', 'string2']" running the code from jenkins.
-
Alexander Suraphel over 2 years@herm tested it myself and you're right. i've updated the answer