1

I have the following piece of code:

String[] arr = [
            "Cat",
            "Dog",
            "Horse"
    ]

String payload = """
{
    "Data" : ${arr}
}
"""

My end goal is to obtain a payload as:

{"Data":["Cat", "Dog", "Horse"]}

But the result I get is:

{"Data":[Cat, Dog, Horse]}

How can I fix this?

2 Answers 2

1

So I had been doing it wrong and found the solution using the help of a friend:

String payload = """
{
    "Data" : ${JsonOutput.toJson(arr)}
}
"""
Sign up to request clarification or add additional context in comments.

1 Comment

Don't partially generate the JSON. Generate the whole thing: JsonOutput.toJson([Data: ["foo", "bar", "baz"]])
0

The groovy way would be simply

String[] arr = [
            "Cat",
            "Dog",
            "Horse"
    ]
String payload = groovy.json.JsonOutput.toJson( Data:arr )

assert '{"Data":["Cat","Dog","Horse"]}' == payload

Comments

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.