Handling complex Variables as JSON and retrieve these via REST API

Hello,

i’ve created a simple bpmn with with the camunda modeler including an external task providing two test variables - a list and a map. After building with maven and deployment, these are shown as type “object” with a value of type “java.util.ArrayList” and “java.util.TreeMap”. I now want to fetch this task and the variables as JSON.
By default i get these variables as base64 encoded Java-serialized object via the REST API like:
“MapTest”: {
“type”: “Object”,
“value”: “rO0ABXNyABFqYXZhLnV0aWwuVHJlZU1hcAzB9j4tJWrmAwABTAAKY29tcGFyYXRvcnQAFkxqYXZhL3V0aWwvQ29tcGFyYXRvcjt4cHB3BAAAAAJ0AAVUZXN0MXQAATF0AAVUZXN0MnQAATJ4”,
“valueInfo”: {
“objectTypeName”: “java.util.TreeMap”,
“serializationDataFormat”: “application/x-java-serialized-object”
}
}

I found this in the user guide: https://docs.camunda.org/manual/7.10/user-guide/data-formats/json/#serializing-process-variables
so i added the tag “” to the camunda.config.xml in my maven project.
Unfortunately, this had no affect on the behaviour of how the variable is formated when fetching the task.

How do i get the variables as json?

Another question is: i also wanted to add a plain JSON variable to my task as a test. So that the real type of the variable in the process engine is “json”. How do i define a json type variable via the modeler? I failed using expressions or inline javascript.

I’m using:
camunda modeler version 4.4.0
camunda process engine version 7.14

Hello @lucia ,
You can serialize your Java objects explicitly as JSON by using the Spin library JSON | docs.camunda.org
or you can set the defaultSerializationFormat to application/json:

Default Serialization Format

The engine can be configured to persist all objects for which no explicit data format is specified as JSON. The process engine configuration offers a property defaultSerializationFormat. To configure default JSON serialization, set this property to application/json. Now, the invocation runtimeService.setVariable(processInstance.getId(), "customer", new Customer()) directly serializes the customer object as JSON without explicit declaration of the format.

Best, McAlm

Hi McAlm,

thanks for your reply. I’ve set the defaultSerializationFormat like mentioned in my post.

runtimeService.setVariable(processInstance.getId(), “customer”, new Customer())

But I don’t want to use JAVA staying inside the process engine - I want to use the REST API. And the defaultSerializationFormat setting seem to have no effect on the responses of the API.
Nevertheless i may take a look at the Spin library. Although i expect that a REST API should expose data in a non technology-bound way by default.

greetings, J.Lucia

What kind of application are you building? Is it Springboot or something else?

I used the plain camunda servlet project template in eclipse/maven and only set the defaultSerializationFormat and changed the bpmn model.
I’ve attached the bpmn file, so therefore you may can reproduce the problem.process.bpmn (6.5 KB)
After starting an instance, i checked the variables with the rest call:
http://localhost:8080/engine-rest/external-task/fetchAndLock
BODY:
{
“workerId”:“aWorkerId2”,
“maxTasks”:10,
“usePriority”:true,
“topics”:
[{“topicName”: “ETTestJava”,
“lockDuration”: 5000
}]
}

Hi @lucia: Please set the defaultSerializationFormt in your bpm-platform.xml in Tomcat. You’ll find this in /server/apache-tomcat…/conf/bpm-platform.xml.
After restarting the defaultSerializationFormat is set and fetchAndLock returns all variables:

[
{
    ...
    "workerId": "aWorkerId",
    "topicName": "ETTestJava",
    "tenantId": null,
    "variables": {
        "MapTest": {
            "type": "Object",
            "value": "{\"Test1\":\"1\",\"Test2\":\"2\"}",
            "valueInfo": {
                "objectTypeName": "java.util.TreeMap<java.lang.Object,java.lang.Object>",
                "serializationDataFormat": "application/json"
            }
        },
        "ETContent": {
            "type": "String",
            "value": "Test",
            "valueInfo": {}
        },
        "ListTest": {
            "type": "Object",
            "value": "[\"LT1\",\"LT2\"]",
            "valueInfo": {
                "objectTypeName": "java.util.ArrayList<java.lang.String>",
                "serializationDataFormat": "application/json"
            }
        }
    },
    ...
 }
]

Best, McAlm

1 Like

Hi McAlm,

thanks for your hint! It works now and the Variables are send i json. I don’t understand why the json is not just plain in the variable value, but quoted in a string…but thats ok for me.
At my reasearch i found this jira post https://jira.camunda.com/browse/CAM-4835
…agreeing with the last comment: Adding the location of the setting to the user-guide would had been nice =)
greeting, JLucia