How to create a JSON object in groovy script from Camunda?

Hi Guys,

I want to create a dynamic json array in groovy script for rest payload.
I am able to take the input array but unable to create the json from it, please help.

def inpuArray = execution.getVariables();

println inpuArray;

def jsonData= []

inpuArray.each { key, value->
println “${key} has the ${value}”
jsonData.add(${key});
};

println jsonData;

output:

Cannot instantiate process definition holiday:1:fb7e1b33-d728-11e9-8996-1062e5902aa5: Unable to evaluate script while executing activity ‘ServiceTask_1’ in the process definition with id ‘holiday:1:fb7e1b33-d728-11e9-8996-1062e5902aa5’: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.$() is applicable for argument types: (Script4$_run_closure1$_closure2) values: [Script4$_run_closure1$_closure2@1522d4df] Possible solutions: is(java.lang.Object), any(), get(java.lang.String), use([Ljava.lang.Object;), any(groovy.lang.Closure), wait()

You can try something like this

import groovy.json.JsonOutput

class O {
    def key
    def val
}

def l = [[key:1, val:2] as O, [key:3, val:4] as O]

print JsonOutput.prettyPrint(JsonOutput.toJson(l))

Refer these links:

https://groovy-lang.org/json.html

Thanks Bro!!
But I am doing this into camunda payload.

I am using camunda payload as a groovy script.