REST message JSON

Hi community.

I would like to ask what would be the best solution to receive a REST message with a JSON data which might surpass the constraint of 4000 characters.

I have tried to escape json like this and it works:

{
“messageName”: “Message_28ot9tb”,
“businessKey”: “businessKey”,
“processVariables” : {
“aVariable” : {“value” : “ESCAPEDJSON”, “type”: “Json”}
}
}

but somehow I dont think this is the best solution,

Thx for any adiice.

M.

Hi Michal,

Using the Json variable type sounds like a decent solution to me.

Cheers,
Thorben

@Michal_S

https://docs.camunda.org/manual/7.8/user-guide/data-formats/json/

https://docs.camunda.org/manual/7.8/reference/spin/json/01-reading-json/

Thnk you @thorben @StephenOTT
for the quick answer.

If I am not mistaken It basically means that in case sender of a REST message cant for some reason Escape a JSON then the only other option is to store a payload in a file and afterwards read payload as SPIN.

thx

@Michal_S you can use a script in your BPMN to escape the json.

Javascript:
S(JSON.stringify(response))

@StephenOTT thx for the suggestion.
I have been reading a lot about spin/josn today as I am still a newbie here. But I cant still find a way how to convert let say a string which came via REST with more than 4000 characters to json/spin, so I can meaningfully work with it.

In your output parameter in your HTTP-Connector configuration set the type of Script and use Javascript: Then execute the script from above.
Then you can save your SPIN. So something like

var mySpin = S(JSON.stringify(response))
execution.setVariable('myResponse', mySpin)

Then when you get that variable somewhere else like in a expression or other scripts you can do

expression example:

${myResponse.prop('myKey').value()}

assuming your json looks like

{
   "myKey": "myValue"
}

then the expression would eval to a value of myValue.

see:

and

2 Likes

thank you again for the answers
the answers are easily understandable
But I am strugling with parsing JSON

I did put
the a mockup json online
http://www.mocky.io/v2/5ac4fad12f00005300f5fcd1

and tried multiple javascript in order to play with the json data but cant get it right


thx for any advice

@Michal_S what is the error you are receiving in the console?

@StephenOTT
I have tried many things and got diffrent resulte
but the last one is
Could not apply modification :
Unable to evaluate script: ReferenceError: “execution” is not defined in at line number 2

You have to use “connector” instead of “execution”. Execution is not available: https://docs.camunda.org/manual/7.8/user-guide/process-engine/scripting/#variables-available-during-script-execution

Please make sure to always post your error messages. Can troubleshoot much faster.

1 Like

@StephenOTT
works like a charm
thx

sorry for bringing this up again and again
basically what I did is
I did successfully import big chunk of json via rest http connector but not stringified
in case I try to stringify I immediately get some kind of errors like

var mySpin = S(JSON.stringify(response));
connector.setVariable(‘var1’, mySpin)
gives
java.util.TreeMap cannot be cast to java.lang.String
or
SPIN-01004 No matching data format detected

change of json array to just plain json {}
var mySpin = JSON.stringify(response);
connector.setVariable('holidays3 ', mySpin)

ENGINE-03083 Exception while executing Batch Database Operations with message ’

Error flushing statements. Cause: org.apache.ibatis.executor.BatchExecutorException:

org.camunda.bpm.engine.impl.persistence.entity.HistoricVariableInstanceEntity.insertHistoricVariableInstance
(batch index #1) failed.
Cause: java.sql.BatchUpdateException: Data truncation: Data too long for column ‘TEXT_’

what do I aim is to do some jsonpath very similiary like

but it seems it wouldnt be possible without stringify… then elements…

Can you please confirm ?

MANY thanks

Strings have a limit in the database of 4000 characters.
YOu need to keep your object as a SPIN JSON which will be stored as a object/byte array/blob and therefore not have the limit of the 4000 characters.

When you stringify you should only be doing this in-memory/within the script or when you need to same more simple data (within 4k characters)

Why do you want to save your JSON as a string?

1 Like

It is not that I want to work with JSON as a string.
What do I aim is to work with JSON using tools like jsonpath, ( elements) .
Which basically means to do it in-memory, if I am not mistaken

Thank you

@Michal_S use the SPIN library to do the Json patch query. And keep your Json as a spin object whenever saving as a variable.

https://docs.camunda.org/manual/7.8/reference/spin/json/03-querying-json/

1 Like