Multi-Instance loop over JSON array

Hi guys,
Ive used multi-instance loops over java collections, however is it possible to configure a multi-instance task over a single process variable which is effectively a JSON array?



Hi Rob,

I haven’t tried this myself yet. Assuming the JSON array variable is a Spin JSON value, it should be possible to write camunda:collection="${variable.elements()}" in the multi-instance configuration.



Thanks Thorben,

worked like a charm!



A post was split to a new topic: Multiple REST requests based on JSON array

Hi @Webcyberrob,

Could you please give more details how you pass a JSON array in the multi-instance collection?

I have a process variable “order” like the following:

(actually I have a list of objects retrieved by the database which I then serialize into a process variable:

ObjectValue ordersDataValue = Variables.objectValue(orderEntities)

I would like my cardinality to be the size of the array.
What should I write in the collection of the multi-instance task? And what in the “Element Variable”?
And then, in each of the multi-instance, how do I access any property, e.g. OrderQty?


@kontrag see: Pattern Review: DMN Looping for Array Input

Hi @StephenOTT,

I had already seen that post but there you set the “collection” variable as
var collection = S('{ "collection" : ["System 1", "System 3"] }');

while I I have set it as:
[{“orderID”:1,“orderNo”:“6RETYY”,“orderQty”:2}, {“orderID”:2,“orderNo”:“45RFFS”,“orderQty”:5}, {“orderID”:3,“orderNo”:“34HJYT”,“orderQty”:3}]

Also, you append the values after the DMN task but how can I append the values?


@kontrag you need to convert your json array into a Java collection. Hence the use of SPIN + the .elements() function.

[quote="kontrag, post:7, topic:349"] Also, you append the values after the DMN task but how can I append the values? [/quote] When you want to append the values, you need to set a delegate or script on the execution `end` listener of your task so that each instance's end gets updated. Its the same model of the DMN pattern.

Any easy way to make the conversion?

Also, I don’t have a multi-instance task like your DMN before my actual multi-instance task.
Why do I need the end listener to update the variable?
Can’t I get it the “combinedResult” variable with the values right after the generation of the collection?

@kontrag use the SPIN library in your java delegate to generate a spin object and then do a .elements()

Sorry, this was mistake on my part; i was thinking of another thread.

1 Like

Hi again,

I have the following string:

String orders = [{“orderID”:1,“orderNo”:“6RETYY”,“orderQty”:2}, {“orderID”:3,“orderNo”:“34HJYT”,“orderQty”:3}]

How do I convert it into JSON?
I try:

 JsonValue orders = SpinValues.jsonValue(orders).create();
        execution.setVariable("orders", orders);`

but I get an error:
Cannot find serializer for value 'Value 'null' of type 'json''.


@kontrag see the spin docs on reading Json string into spin:

1 Like

Thanks @StephenOTT!

I simply wrote
String orders_str = [{“orderID”:1,“orderNo”:“6RETYY”,“orderQty”:2}, {“orderID”:3,“orderNo”:“34HJYT”,“orderQty”:3}]

SpinJsonNode orders = S(orders_str, json());

and worked :wink:

@kontrag, Can you please tell me how you have configured the multiinstance in your bpmn. I am also trying to implement something like this. Also how do you access each object in the json and use them in the bpmn user task? Any help is much appreciated.

@ChandruM take a look at Pattern Review: DMN Looping for Array Input