0

I'm working in a declarative pipeline, and I have a string that looks like this:

'[[key_A:value1, key_B:value2, key_C:value3],[key_A:value4, key_B:value5, key_C:value6],[key_A:value7, key_B:value8, key_C:value9]]' 

Can I get help on what the quickest way is to turn the string into a map, then retrieve values of each map in the arraylist?

2
  • 1
    Think to use json format to transfer list/map between steps and use steps readjson and writejson to convert string to objects Commented Nov 2, 2021 at 18:44
  • If you really got this as String (and this is not just you printing things and seeing it this way), then you should eliminate the place, that "toString"s your map here. .toString() is unsuitable as a serialization format. This quickly starts to break, if your keys or values contains whitespaces, colons, ... Commented Nov 2, 2021 at 19:47

1 Answer 1

1

Can I get help on what the quickest way is to turn the string into a map, then retrieve values of each map in the arraylist?

The input string you provide doesn't look like a map, it looks like a list of map. You can turn the string into a list of map using something like this (notice that the values here are quoted so they are strings, otherwise you would have to provide variables for value1, value2 etc):

 def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]' def inputList = Eval.me (inputString) 

Then you could iterate over that list to retrieve the maps and do whatever you want to do with the values in the maps:

 def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]' def inputList = Eval.me (inputString) inputList.each { Map m -> println m.values() } 
Sign up to request clarification or add additional context in comments.

5 Comments

I just realized the list of maps I am dealing with does not have quotes around the values. It looks like this: [[key_A:ab-32, key_B:23232, key_C:23dd],... As you said, using eval will prompt an error. is there a solution to this? @JeffScottBrown
@HCLW Yes: never ever use .toString() as a serialization format. Try to find the place, that messes this up and use JSON or some other format you facy. What you are looking for will only ever work properly with a narrow set of inputs.
You and everyone else commenting were correct. I saved the json string to a file and tried to read back from the file, hence the string. @cfrick Thanks
"I saved the json string to a file and tried to read back from the file" - Is value expressed in the question valid JSON?
@HCLW "As you said, using eval will prompt an error. is there a solution to this?" - Can you describe what values you want the variables (value1, value2, value3 etc.) to have?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.