0

Can anyone help to convert a list to list of Maps in Groovy ?

I have a list as below

[war, jar]

and a variable with name "value" = 2.0.0

I want to convert this into a list with maps [["pack": "war", "ver": "2.0.0"],["pack": jar, "ver": "2.0.0"]]

and create a json.

{
    "ProjectId": "Projects-16",
    "ChannelId": "Channels-41",
    "Version": "2.0.1.0-10",
    "selectedPackages": [{"pack": "war", "ver": "2.0.0"}, {"pack": jar, "ver": "2.0.0"]}]
}
6
  • Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. Have you tried collect or collectEntries? Commented Feb 16, 2021 at 11:45
  • Also ["war": "2.0.0", "jar": "2.0.0"] is not the same as [{"war": "2.0.0"}, {"jar": "2.0.0"}] Commented Feb 16, 2021 at 11:46
  • yes. I tired collect and able to form ["war": "2.0.0", "jar": "2.0.0"] but i'm not sure how i can convert everything into json. Commented Feb 16, 2021 at 11:50
  • Then why is your question phrased like you have problems building a map/list of maps, when your actual problem is something different? Have you looked into JsonOutput to create JSON? Commented Feb 16, 2021 at 11:52
  • Because, i'm not sure if the map I created is correct or not. As you pointed out ["war": "2.0.0", "jar": "2.0.0"] is not the same as [{"war": "2.0.0"}, {"jar": "2.0.0"}]. I also tried list.collect { it -> "{$it: ${value}}" } which gives [{jar: 4.0}, {war: 4.0}]. But when i used JsonBuilder to convert into json, it converts as { "channelId": "Channels-41", "selectedPackages": [ "war: 2.0.0.","jar:2.0.0" ], "version": "2.0.0.7-10", "projectId": "Projects-16" } Commented Feb 16, 2021 at 12:05

1 Answer 1

1

Here is the solution:

import groovy.json.JsonOutput

def packages = ["war", "jar"]
def value = "2.0.0"
def list = packages.collect { [pack : it, ver : value] }
def object = [
    ProjectId: "Projects-16",
    ChannelId: "Channels-41",
    Version: "2.0.1.0-10",
    selectedPackages: list
]
def json = JsonOutput.toJson(object)
println JsonOutput.prettyPrint(json)

Each {} entity in JSON is a map in Groovy therefore the expected [{}, {}] is a list of maps. You need just to construct the right objects using collect with the right transformation.

Edit
I updated the solution to return the exact json output as desired using a list and the variable value.

The output is this:

{
    "ProjectId": "Projects-16",
    "ChannelId": "Channels-41",
    "Version": "2.0.1.0-10",
    "selectedPackages": [
        {
            "pack": "war",
            "ver": "2.0.0"
        },
        {
            "pack": "jar",
            "ver": "2.0.0"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's no problem here, but GStrings make bad map keys. Better use [(it): ...] instead.

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.