Multi-Instance JSON Collections in javascript using a script task

Take a look at this thread:

It walks through how to use Multi-instance with arrays, and there are json examples. The example is for DMN, but it is the same configuration setup for your use case.

Also take a look at:

var arr = [ "hello", "world" ];
for each (a in arr) {
 print(a)
}

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

in the “For Each Expression” section. You can simplify your code using the Nashorn extensions.

In general it is must more clear and dev friendly to build your objects in their native forms (such as js) and then only convert to SPIN at the very end.

So you do not need to have var jsonValue = S('{ "values":[] }');. You can just create a js array, and build your list. When you do setVariable() then you can set your SPIN. Something like S(JSON.Stringify(jsonValue)) where jsonValue is a regular js array or object.

My recommendation is to rebuild from the examples above and the examples in teh DMN Looping link.