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]
Share:
76,715

Related videos on Youtube

user2068981
Author by

user2068981

Updated on July 09, 2022

Comments

  • user2068981
    user2068981 almost 2 years

    How can I convert this String variable to a List ?

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

    I tried with: as List and toList();

    • Admin
      Admin about 11 years
      It is already a list in groovy?
    • Reimeus
      Reimeus about 11 years
      This is a List do you want to convert it to a String?
    • user2068981
      user2068981 about 11 years
      But when I do ids.each{println it} I have this result : [</br> 1</br> 0</br> ,</br> </br> 1</br> ]</br>
  • Sergio Martinez
    Sergio Martinez about 11 years
    I think you want to make a string "10,1,9" into a list [10,1,9]
  • user2068981
    user2068981 about 11 years
    def id = ids.substring(1,ids.length()-1) def l= id.split(',').collect{it as int}
  • user2068981
    user2068981 about 11 years
    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}
  • Alexander Suraphel
    Alexander Suraphel about 7 years
  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- over 5 years
    @AlexanderSuraphel Wrong language.
  • Alexander Suraphel
    Alexander Suraphel over 5 years
    @chrylis Meaning?
  • Michael A.
    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
    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
    Alexander Suraphel over 5 years
    I think a better link is stackoverflow.com/questions/86513/…. The ideas discussed apply to any language, not just JavaScript.
  • CIsForCookies
    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
    Vladimir over 4 years
    The higher scoring answers didn't work for me. This one did.
  • Pavol Travnik
    Pavol Travnik about 4 years
    I would not consider using Eval as a safe option though.
  • Pavol Travnik
    Pavol Travnik about 4 years
    This should definitely be accepted as right answer.
  • Max Cascone
    Max Cascone over 3 years
    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'
  • Max Cascone
    Max Cascone over 3 years
    updating mine after the edit window closed: list = versionFile.split(',').collect() works fine for strings.
  • herm
    herm over 2 years
    For me with strings this only works when I have : '["string1", "string2"]' and not with "['string1', 'string2']" running the code from jenkins.
  • Alexander Suraphel
    Alexander Suraphel over 2 years
    @herm tested it myself and you're right. i've updated the answer