31

How can I convert this String variable to a List ?

def ids = "[10, 1, 9]"

I tried with: as List and toList();

3
  • It is already a list in groovy? Commented Feb 28, 2013 at 0:05
  • This is a List do you want to convert it to a String? Commented Feb 28, 2013 at 0:09
  • But when I do ids.each{println it} I have this result : [</br> 1</br> 0</br> ,</br> </br> 1</br> ]</br> Commented Feb 28, 2013 at 0:15

4 Answers 4

36
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.

Sign up to request clarification or add additional context in comments.

7 Comments

@AlexanderSuraphel Wrong language.
@chrylis Meaning?
@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.
I would not consider using Eval as a safe option though.
|
33
def l = ids.split(',').collect{it as int}

5 Comments

I think you want to make a string "10,1,9" into a list [10,1,9]
def id = ids.substring(1,ids.length()-1) def l= id.split(',').collect{it as int}
I 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}
this 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'
updating mine after the edit window closed: list = versionFile.split(',').collect() works fine for strings.
31

Use the built-in JsonSlurper!

Using Eval could be risky as it executes any code 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]

3 Comments

This should definitely be accepted as right answer.
For me with strings this only works when I have : '["string1", "string2"]' and not with "['string1', 'string2']" running the code from jenkins.
@herm tested it myself and you're right. i've updated the answer
18

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]

4 Comments

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]'"
The higher scoring answers didn't work for me. This one did.
Could you explain what exactly tokenize(',[]') doing here?
Without the brackets, you get [[a, b, c]] as a result. Not sure I understand how adding the brackets removes the additional outer list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.