org.camunda.spin.impl.json.jackson.JacksonJsonNode cannot be cast to java.lang.String

Hi,

This might be a very obvious question, but as a beginner I don’t seem to get it.
I’m trying to construct the payload of my POST request as a json object. It should look like:

{“destinations”: “…” , “modelparameters”: “…”, “inputparameters”: “…”}

RN my payload parameter looks like:

<camunda:inputParameter name=“payload”>
<camunda:script scriptFormat=“Javascript”>
var string = ‘{“destinations”: “null”}’
var json = S(string)
S(json).prop(“modelparameters”, execution.getVariable(“modelparameters”))
S(json).prop(“inputparameters”, inputparameters)
json
</camunda:script>
</camunda:inputParameter>

My error is org.camunda.spin.impl.json.jackson.JacksonJsonNode cannot be cast to java.lang.String

Can someone give me a quick heads up what i’m doing wrong?

Cheers

Your Json string has to be stringified before import into spin. Basically spin will only accept a string that has no unescaped white space.

Do: S(JSON.stringify(mystring))

Your lines after would just be “json.prop(‘myKey’)…”. You don’t need to keep reapplying the S()

1 Like

Thank you for your response Stephen.

However, I still dont get it to work after changing S(string) to S(JSON.stringify(string))
string is still of form ‘{“X”:“Y”}’

“SPIN-01004 No matching data format detected”

15-Jul-2018 11:25:52.877 WARNING [http-nio-8080-exec-6] org.camunda.bpm.engine.rest.exception.ExceptionHandler.toResponse org.camunda.spin.spi.SpinDataFormatException: SPIN-01004 No matching data format detected
at org.camunda.spin.impl.logging.SpinCoreLogger.unrecognizableDataFormatException(SpinCoreLogger.java:53)
at org.camunda.spin.impl.SpinFactoryImpl.createSpinFromReader(SpinFactoryImpl.java:127)
at org.camunda.spin.impl.SpinFactoryImpl.createSpin(SpinFactoryImpl.java:50)
at org.camunda.spin.impl.SpinFactoryImpl.createSpinFromString(SpinFactoryImpl.java:103)
at org.camunda.spin.impl.SpinFactoryImpl.createSpin(SpinFactoryImpl.java:47)
at org.camunda.spin.Spin.S(Spin.java:64)
at jdk.nashorn.internal.scripts.Script$29$^eval_.:program(:2)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)

Please provide a full example of the Json you are submitting

1 Like

The JSON should look like this:
{ “destinations”:
[“http://10.1.1.2:8008”,
http://10.1.1.3:8008”,
http://10.1.1.4:8008”,
http://10.1.1.5:8008”,
http://10.1.1.6:8008”,
http://10.1.1.7:8008”]
,“inputparameters”:
“null”
,“modelparameters”:
{ “hiddenlayer”: [100,100,100]
, “num_input”: 784
, “num_classes”: 10
, “learning_rate”: 0.15
, “num_steps”: 12
, “batch_size”: 100}

}
“destinations” is a list variable, set at the start of the process
“modelparameters” is another json object, with its elements set in a generated task form. In the output parameters of that user task i used the following javascript:
var string = ‘{“hiddenlayer”: “tim”}’
var json = S(JSON.stringify(string))
json.prop(“hiddenlayer”, hiddenlayer)
json.prop(“num_steps”, num_steps)
json.prop(“learning_rate”, learning_rate)
json.prop(“batch_size”, batch_size)
json.prop(“num_classes”, num_classes)
json.prop(“num_input”, num_input)
json

Then in my next task I build the whole json object through a javascript for the payload variable:

var string = ‘{“inputarameters”: “null”}’
var json = S(JSON.stringify(string))
json.prop(“modelparameters”,execution.getVariable(“modelparameters”))
json.prop(“destinations”, execution.getVariable(“destinations”)
json

I tried setting the payload that I want as a whole as a string as payload:

var string = ‘{ “destinations”: [“http://10.1.1.2:8008”, “http://10.1.1.3:8008”, “http://10.1.1.4:8008”, “http://10.1.1.5:8008”, “http://10.1.1.6:8008”,“http://10.1.1.7:8008”], “inputparameters”: “null” ,“modelparameters”: {“hiddenlayer”: [100,100,100],“num_input”: 784,“num_classes”: 10, “learning_rate”: 0.15, “num_steps”: 12,“batch_size”: 100}}’

If I try S(string) I get error: org.camunda.spin.impl.json.jackson.JacksonJsonNode cannot be cast to java.lang.String
If I try S(JSON.stringify(string)) I get error: “SPIN-01004 No matching data format detected”

Build your Json object in Javascript, and only at the end do a conversion into SPIN.

Also check your quotes. You seem to be use curly quotes. Can you make sure to use three backticks ``` to wrap your code examples to show the exact formats you are using.

var myJson = {
   "hiddenlayer": hiddenlayer,
   "num_steps": num_steps,
   "learning_rate": learning_rate,
   "batch_size": batch_size,
   "num_classes": num_classes,
   "num_input": num_input
}

var myJsonString = JSON.stringify(myJson)
var mySpin = S(myJsonString)
execution.setVariable('mySpinVariableName', mySpin)
1 Like

I am also getting the error:

class org.camunda.spin.impl.json.jackson.JacksonJsonNode cannot be cast to class java.lang.String (org.camunda.spin.impl.json.jackson.JacksonJsonNode is in unnamed module of loader java.net.URLClassLoader @185d8b6; java.lang.String is in module java.base of loader ‘bootstrap’)

I have defined a multi instance user task like this:

The variable “angestellte” in the cockpit has the type Json and the following value:

["hans","max","johannes","steffen"]

Because the variable is of type Json I don’t have to use S(...) correct? So where is my mistake?