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?

regards

Rob

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.

Cheers,
Thorben

4 Likes

Thanks Thorben,

worked like a charm!

regards

Rob

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:
[{“orderID”:1,“orderNo”:“6RETYY”,“orderQty”:2},
{“orderID”:2,“orderNo”:“45RFFS”,“orderQty”:5},
{“orderID”:3,“orderNo”:“34HJYT”,“orderQty”:3}]

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

ObjectValue ordersDataValue = Variables.objectValue(orderEntities)
                    .serializationDataFormat(Variables.SerializationDataFormats.JSON)
                    .create();
            execution.setVariable("orders",ordersDataValue);

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?

Thanks!

@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?

Thanks!

@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''.

Thanks!

@kontrag see the spin docs on reading Json string into spin: https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/#reading-json-from-a-string

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