'Adding new Object in Spin Object Camunda

I am creating a new variable in Camunda

 var userData = [
    {
        name: "rajesh",
        age: 40
    },{
        name: "aara",
        age: 4
    }
]

execution.setVariable("userData", S(JSON.stringify(userData)).mapTo("java.util.ArrayList"));

I want to add a new Object into it, how can i do that?

newUser = {
name: "vijay",
age: 35
};

userData.add(JSON.stringify(newUser))    
print(userData)

This gives me output like this : [{"name":"rajesh","age":40},{"name":"aara","age":4},"{\"name\":\"vijay\",\"age\":35}"]

Please help



Solution 1:[1]

Did you see the docuemntataion for it https://docs.camunda.org/manual/latest/user-guide/data-formats/json/ ? Waht is unclear / failing?

Solution 2:[2]

Thouth it is an old question, I faced the same one recently. Here is the solution.

  1. let the variable be org.camunda.spin.impl.json.jackson.JacksonJsonNode but java.util.ArrayList, therefore .mapTo("java.util.ArrayList") is unnecessory;

  2. Use JacksonJsonNode#append method to add a new element, thus the code may look like

var newUser = {
name: "vijay",
age: 35
};

S(userData).append(S(JSON.stringify(newUser)));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rob2universe
Solution 2 Tonny Tc