ExternalTaskService.complete() - how to set a variable of type JSON

I have a process instance where a variable is set via REST.
The variable is of type JSON and the content is a Json-String { … }

Completing a task I want to change this variable and I don’t find a way to do this without loosing my type JSON.

// => result is a variable of type String
String jsonString = “{…}”
HashMap<String, Object> map = new HashMap<String, Object>(1);
map.put(“image_data”,(Object)jsonString);
externalTaskService.complete(externalTask,map)

// => result is of type Object containg a java.lang.String
String jsonString = “{…}”
VariableMap variableMap =
Variables.createVariables()
.putValue(“image_data”, Variables.objectValue(jsonString,true));
externalTaskService.complete(externalTask,variableMap);

how can I store a process instance variable of type JSON in external-task-client ?

Thanks a lot

1 Like

You should be using the Camunda SPIN library. You set your “object” as a SPIN JSON variable, and it will be stored as a “json” type.

Hi @tpelzer,

try this statement:
ObjectValue typedCustomerValue = Variables.objectValue(customer).serializationDataFormat("application/json").create();

You can find more details here: https://docs.camunda.org/manual/7.10/user-guide/data-formats/json/

Hope this helps, Ingo

I am using SPIN first time …
It works generally but it seems that I am doing something wrong

JsonValue jsonValue = SpinValues.jsonValue(jsonString).create()

=> jsonValue:
+) type is “json”
+) isDesirialized = false
+) the SerializedValue is provided with the content of my jsonString
+) but the Value itself is null

completing the task with:

map.put(“data”, jsonValue);
externalTaskService.complete(externalTask,map);

=> rsults in error:
org.camunda.bpm.client.exception.ValueMapperException: TASK/CLIENT-01023 Cannot find serializer for value 'Value ‘null’ of type ‘json’

is there any example of a successful use of SpinValues ?

Thanks

1 Like

Ingo,
is it necessary to start the Process Instance ? in my scenario, process is also started and as part of the external task i receive a big data as payload and i want to persist the data in JSON format , how can i achieve that ?

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(“aProcess”);

ObjectValue typedCustomerValue =

  • Variables.objectValue(customer).serializationDataFormat(“application/json”).create();*

runtimeService.setVariable(processInstance.getId(), “customer”, typedCustomerValue);
```