How to iterate over SpinList with javascript


#1

I have to iterate over a SpinList. Here is my code to fetch one Object from SpinList

var serviceOrders = S(response).elements();
var order = serviceOrders.get(0);
var name = order.prop(“name”);
var address = order.prop(“address”);
print(name+""+address);
S(response);

This works fine. However the code below fails.

for (var order in serviceOrders) {

var name = order.prop(“name”);
var address = order.prop(“address”);
print(name+""+address);
}

}

How can I iterate and print all the objects with a for loop.


#2

@prasadps just use JSON.parse(response); and convert to a Spin object at the end of your script when you want to store the data.


#3

Hi,
I have JsonNode which I get the SpinList out of it.
I iterate over the list, do some stuff (remove some elements) and then I want to save the new SpinList as process variable.
I do the following:

   SpinJsonNode orderlines_json = (SpinJsonNode) execution.getVariable("orderlines_json");
   SpinList orderlines_list = orderlines_json.elements();

    for (Iterator<SpinJsonNode> it = orderlines_list.iterator(); it.hasNext();) {
        SpinJsonNode orderline = it.next();
        if (orderline.prop("orderlineType").toString().equals("book"))
        {
            it.remove();
        }
    }

    ObjectMapper mapper = new ObjectMapper();
    //mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);
    String arrayToJson = mapper.writeValueAsString(orderlines_list);
    SpinJsonNode neworderlines_json = S(arrayToJson, json());

    execution.setVariable("new_orderlines",neworderlines_json);

but the process variable I get is:
[{"array":false,"null":false,"nodeType":"OBJECT","value":false,"string":false,"object":true,"number":false,"boolean":false,"dataFormatName":"application/json"},{"array":false,"null":false,"nodeType":"OBJECT","value":false,"string":false,"object":true,"number":false,"boolean":false,"dataFormatName":"application/json"}]

What do I do wrong?
Actually, how do I convert a SpinList into SpinJsonNode?


#4

Why do you create the SpinJsonNode?

Why not just do: execution.setVariable("new_orderlines",orderlines_list ); ? the orderlines_list variable had your “book” types removed. So you should be able to just add this back into Camunda.


#5

Hi @StephenOTT,

I had tried that and I get
Cannot serialize object in variable 'new_orderlines': org.camunda.spin.impl.json.jackson.JacksonJsonNode

My initial process variable is
[{"orderlineId":3,"orderlineNo":"67687","orderlineApplicationType":"book","orderlineDueDate":"2018-03-04"},{"orderlineId":4,"orderlineNo":"45454","orderlineApplicationType":"magazine","orderlineDueDate":"2018-03-04"},{"orderlineId":5,"orderlineNo":"3435","orderlineApplicationType":"magazine","orderlineDueDate":"2018-03-04"}] which is of type Json in Camunda.


#6

Can you print out the variable after it has gone through the loop? Would like to confirm its contents


#7

After the loop, the orderlines_list is correct:
[{"orderlineId":4,"orderlineNo":"45454","orderlineApplicationType":"magazine","orderlineDueDate":"2018-03-04"},{"orderlineId":5,"orderlineNo":"3435","orderlineApplicationType":"magazine","orderlineDueDate":"2018-03-04"}]


#8

Not sure then. sorry


#9

What I did and it worked:

When I find a JsonNode in my list that I want to remove, I simply remove it from the SpinJsonNode variable (also from the list but not necessary). Then this variable has the updated items and I set it again as execution variable (with different name, maybe it also works if it’s the same as I got it).
So the following code works:

SpinJsonNode orderlines_json = (SpinJsonNode) execution.getVariable("orderlines_json");
SpinList orderlines_list = orderlines_json.elements();

for (Iterator<SpinJsonNode> it = orderlines_list.iterator(); it.hasNext();) {
    SpinJsonNode orderline = it.next();
    if (orderline.prop("orderlineType").toString().equals("book"))
    {
        orderlines_json.remove(orderline);
        it.remove();
    }
}

execution.setVariable("new_orderlines",orderlines_json);

#10

Hi,

Something strange happened. It’s not Camunda related, but just in case someone knows.
I had an arraylist
[{“orderNo”:“63NN6”,“orderDate”:“2018-03-01”,“orderId”:8,“orderClient”:‘LibraryA’}, {“orderNo”:“67RRT”,“orderDate”:“2018-03-01”,“orderId”:3,“orderClient”:‘LibraryB’]

I have a multi-instance Call Activity and I iterate over the arraylist as collection.

The Element variable would be for example:
{“orderNo”:“63NN6”,“orderDate”:“2018-03-01”,“orderId”:8,“orderClient”:‘LibraryA’}

and in the Cockpit it was shown as Serialization Data Format: application/json

However, I tested again my process and I noticed that the element variable is of application/xml now!!
and the data are:
image
That would not be a problem but for some reason some properties (e.g. OrderDate) are missing from the xml!!
Any ideas?

The only change I made in my IDE was to set the Target byte code version to 8 (before it was 1.8 but now there’s only the option of 8).


#11

Edit: I removed the Per-module byte code version and added it again (now it says “Target byte code version - Same as language level”) and seems to be fine (the element variable is again json).


#12

Hi,

I face the same issue again.
Does anyone know how the Element variable in the modeler serializes the object? Is there a default serialization format? So, far I was getting application/json (my collection is application/json) but now I get application/xml without changing anything!

Thanks!