Sequence Flow Expression ... Check for empty Array

Hello,
I am trying to figure out how to check for an empty array while doing a conditional check. Here is the structure of the JSON object in Camunda Cockpit.

{“artifacts”: [{“id”: “1”,“status”: “not_started”},{“id”: “2”,“status”: “not_started”},{“id”: “3”,“status”: “not_started”}]}

As I go through each of the items, I am getting a hold of each item (Javascript Script Task):

var artifacts = execution.getVariable(“artifacts”);
var currentItem = artifacts.elements().get(0);
execution.setVariable(“currentItem”, currentItem);

Then, in a separate step, I remove the currentItem from the “artifacts” object doing this:
var artifacts = execution.getVariable(“artifacts”);
var item = execution.getVariable(“currentItem”);
artifacts.remove(item);
execution.setVariable(“artifacts”, artifacts);

In the end, the “artifacts” object looks like this: []

However, when I am trying to check the length of the “artifacts” object array in a sequence flow expression, I always get an error.
"org.camunda.bpm.engine.ProcessEngineException: Unknown property used in expression: ${S(artifacts).length > 0}. Cause: Could not find property length in class

org.camunda.spin.impl.json.jackson.JacksonJsonNode"
Here are some of the things I have tried:
${artifacts.length > 0}
${artifacts.elements().length > 0}
${S(artifacts).length > 0}

Thanks.

@Jerome_X_Jourdon, In ES5+:

if (Array.isArray(artifacts) && artifacts.length) {
    // artifacts exists and is not empty
}

(or)

if (typeof artifacts != "undefined" && artifacts != null && artifacts.length != null && artifacts.length > 0) {
    // artifacts exists and is not empty
}

Try like this below:

var json = S('{"customer": ["Kermit", "Waldo"]}');
var customerProperty = json.prop("customer");
var customers = JSON.parse(customerProperty.elements().toString());
if (typeof customers != "undefined" && customers != null && customers.length != null && customers.length > 0) {
    var customer = customers.get(0)
	var customerName = customer.value();
}
1 Like

Thank you so much. Your example helped me figure out a better way to accomplish things.