Spin: add Jackson JSON array to task variables and evaluate it in Expressions

Here is an example without using Spin… while, still using Jackson. You’ll notice that Jackson sits in a different package within the Spin library - so, it might get a little confusing.

Using Jackson directly:

// create a list of case Ids for return
List<String> caseIds = new ArrayList<String>();
for(CaseInstance caseInstance :  caseInstances) {
  caseIds.add(caseInstance.getId());
} 

// this time we build a proper JSON return value - using Jackson
ObjectMapper mapper = new ObjectMapper();
ObjectNode rootObjectNode = mapper.createObjectNode();

// create the JSON node to hold the BPM returned variables
// ObjectNode processInstanceList = mapper.createObjectNode();
ArrayNode caseInstanceList = mapper.createArrayNode();
// attach to parent
rootObjectNode.set("case IDs", caseInstanceList);
for (String caseId : caseIds) {
  caseInstanceList.add(caseId);
}

This example is a little more fluent - while setting variables at the the root level:

// this time we build a proper JSON return value - using Jackson
ObjectMapper mapper = new ObjectMapper();
ObjectNode rootObjectNode = mapper.createObjectNode();

// Extract completed process results or output
// set the process processID, InstanceID, and isEnded 
// variables as child JSON nodes
rootObjectNode
    .put("processID", processID)
    .put("processInstanceID", piid)
    .put("isEnded", isEnded);

If you’re using JSON types as process variables - recommend testing. I typically parse out what I need and use Camunda’s built-in types.

1 Like