JSON variables passed to REST Call is not serialized correctly

Hello,
i got some process variables that are typed as json. Now, when i fetch them via the runtimeService and pass them to the return of a rest call, i get this output:

{
“nodeType”: “OBJECT”,
“object”: true,
“boolean”: false,
“dataFormatName”: “application/json”,
“string”: false,
“value”: false,
“number”: false,
“array”: false,
“null”: false
}

    @GetMapping("/status/{processInstanceId}")
    public ResponseEntity<?> getProcessInstanceStatus(
            @PathVariable("processInstanceId") String processInstanceId){

            var response = runtimeService.getVariable(processInstanceId, "jsonVariable");
            return ResponseEntity.ok(response);

    }

Same goes with the getVariables call.

Hello my friend!

When you search for a process variable in Camunda, you do not directly receive a JSON object, but rather an object that “represents” that variable and you will need to convert it to the desired format.

For example, if your variable is of type String, you need to use a cast in your code for it to work and actually receive a String.

When you want to search for a JSON process variable in Camunda, you need to bring it… store it in a variable as you did in your response, and convert it into a JSON String using ObjectMapper.

Example in your code:

 @GetMapping("/status/{processInstanceId}")
 public ResponseEntity<?> getProcessInstanceStatus(
 @PathVariable("processInstanceId") String processInstanceId){

 var response = runtimeService.getVariable(processInstanceId, "jsonVariable");

ObjectMapper objectMapper = new ObjectMapper();

var responseJson = objectMapper.writeValueAsString(response);
 return ResponseEntity.ok(responseJson);
 }

Hope this helps.

William Robert Alves

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.