Spin JSON .getVariableTyped("var", false), not returning raw string?

anyone can explain this engine behaviour?

This page: https://docs.camunda.org/manual/latest/user-guide/data-formats/json/
has the following line:

When retrieving the JSON value via execution.getVariableTyped() there are two options: serialized and deserialized. Retrieving the variable deserialized by calling either getVariableTyped(“name”) or getVariableTyped(“name”, true), the JsonValue contains the wrapped Jackson object to represent the JSON data. Calling getVariableTyped(“name”, false) results in JsonValue containing only the raw string, which is advantageous if you only need the string, e.g., to pass it to another API.

snippet of test code:

string that is passed into a form field { "value": 1 }

var jsonVar = execution.getVariable("jsonValue");
var jsonSpin = S(jsonVar);
execution.setVariable("spinVariable", jsonSpin);

execution.getVariableTyped("spinVariable", false);

Line 1 gets a String.
Line 2 converts the strong to a SPIN json object.
Line 3 sets the variable not as a JSON object.
and Line 4 should return the variable as a raw string? Thats what the docs suggest?

Calling getVariableTyped(“name”, false) results in JsonValue containing only the raw string, which is advantageous if you only need the string, e.g., to pass it to another API.

But when this script executes, the returned variable from this script (in a script task) still gets stored as a JSON spin object.

Am i not understanding the docs on this Spin / engine feature?

@camunda

Thanks!

Anyone know about this issue or have a another workaround?

My solution has been to use get the serialized valued… and convert to string. but for sure feels like a hack…

Hi Stephen,

Have you tried to use the toString() method on the JSON object?

execution.getVariable(“spinVariable”).toString()

regards

Rob

@Webcyberrob that works, but still seeing the same problem that we found with another solution:

{
  "value1": "value1Answer",
  "value2": {
    "value3": "{\"value\":1}"
  }
}

Value3 is the spin json object we convert with toString The values are escaped.
Looking for a way to get the unescaped raw text.

Edit:

But you made me think of another way: Convert all the individual objects into SPIN json and then merge the object using the spin api and then at the end convert the spin object with toString().

Yup that worked:

{
  "value1": "value1Answer",
  "new_object": {
    "value": 1
  }
}

The original problem was that json spin object that I was trying to embed within the JS object was not compatible when converting to string or using JSON.stringify. (guess conflicting classes and inability to convert/map between them).

Pattern solution is something such as:

//convert a string of json into a Spin json object
var jsonSpin = S(someStringVariable);

// create a new JS object to hold and manipulate some data
var object1 = {
"value1": "value1Answer"
}

//The JS object is converting to a String and turned into a SPIN JSON object.
var object2 = S(JSON.stringify(object1));

// merge the jsonSpin variable into the object2 Spin object
object2.prop("new_object", jsonSpin);

//convert entire json spin object into a string
object2.toString();
// result is a unescaped json string.
1 Like

@Webcyberrob thanks for the soundboard