Loop over JSON-Array passed by REST

Hi,

I don’t get the following procedure to work; I tried different things I found here but I always end up in the error “Unable to evaluate script: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.var() is applicable for argument types: (org.camunda.spin.impl.json.jackson.JacksonJsonNode)” even with the examples provided on the documentation.

  1. The engine is passed a variable containing JSON including an items-array via external task
  2. The engine should then loop over each of the items and execute a sub-process

Simplified, I pass the following JSON to the complete-endpoint for step 1:
{
“workerId”: “aWorkerId”,
“variables”: {
“data”: {
“type”:“String”,
“value”: “{"items": [{ "id": "1", "name": "a" },{ "id": "2", "name": "b" },{ "id": "3", "name": "c" }]}”
}
}
}

How can I then transform my string properly such that Camunda can loop over the items?

Thank you for helping out, I’m stuck,
Matthias

PS: Not a Java-Dev :wink:

1 Like

@mhelling you Json is currently a String. YOu need to convert it into JSON and then access the collection.

So instead of using String using Json as your Type. and make sure your value is Stringified JSON. Using type Json converts your stringified json into a Camunda SPIN JSON object.

Then in camunda you can access your array with something like myVarName = myJson.prop('items').elements() and this will return you a Collection.

https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/#fetch-array-of-data

1 Like

Thanks, it seems to work now. Must’ve been the only combination I haven’t tried. Using the type Json with the S-Method (“S(data).prop(‘items’).elements()”) didn’t work for instance, so I switched to string.

How are you trying to loop over your items when using S()?

Your “data” needs to be stringified JSON. So if you imported your “data” as a string of json with spaces and other unescaped characters you need to do something like (for JS) S(JSON.stringify(data)).prop('items').elements()

hi! I tried to implement your hints but was unable to succeed. We’re mostly using the REST API and external tasks in Python. Java expertise is not available.

To start a multi-instance we need to use a “collection”. The variable we try to transform into a collection is pushed via REST API and contains an array of strings.

{"sids": ["084ddece-0025", "5dd55df7-0025", "8387952d-0025"]}

Within the modeler we set an output variable like this:

${myvar.prop('sids').elements()}

which fails with the following error:

Cannot serialize object in variable 'mycollection': org.camunda.spin.impl.json.jackson.JacksonJsonNode

Do we miss anything here, like type conversion for (java) collections?
Appreciate your help. Hans

@homat You may try putting the below thing in Variable Assignment Value field:

${myvar.props("sids").mapTo("java.util.ArrayList")}

I also faced this issue and could get it working after struggling for around 2 days.

1 Like

I am at the same point and cannot figure out how to make progress here. I am getting a list from REST call and want to output a list to use to control multi-instance process step. Animesh’s post was of no help as it cannot find any mapTo method

Could you help me at this point? I have a String like that: “{“availability”:[{“end_date”:“2021-12-27T05:00:00+00:00”,“region”:“US”,“type”:“svod”,“start_date”:“2021-12-17T05:00:00+00:00”,“devices”:[“web”,“living-room”,“mobile”,“mvpd”],“change_effect”:“no_change”}]}” but I need it as JSON type to pass a Collection ${variable.prop(“availability”).elements()} and I got ${variable.prop(“availability”).elements()}. Cause: Cannot find method prop with 1 parameters in class java.lang.String at org.camunda.bpm.engine.impl.el.JuelExpression.getValue(JuelExpression.java:65) at. Is there any way to cast from String to JSON type?

Thanks alot!