Generating a Business Key from an JSON string variable

As part of a Call Activity, we need to generate an applicable Business Key from a variable that’s passed in when instantiating the parent workflow instance. So for example, given a variable says objects:

{“objects”: [{“id”: “48d00a32-c7da-4171-bb4c-dfde75a418c2”, “type”: “box”},
{“id”, “5c9a44c6-85c0-4176-8336-b13111a5aded”, “type”: “content”}]}

What we need to do in one case is generate a business key based on the first element in the list. To send the objects variable value into Camunda, we encode it as a string and just set the type to String. Tried using Object and maybe that would be better but ran into problems with setting the objectTypeName and serializationDataFormat.

Fortunately, using Spin we can deserialize the string back to JSON although it’s then in a Jackson JSON Node instead.

So what we want as an end result is this:

48d00a32-c7da-4171-bb4c-dfde75a418c2

This doesn’t work
#{S(execution.getVariableTyped(“objects”))[0][“id”]}

Cause:
Unknown method used in expression: #{S(execution.getVariable("objects")).get(0).prop("id")}. Cause: Cannot find method get with 1 parameters in class org.camunda.spin.impl.json.jackson.JacksonJsonNode

How then can this be used to extract the required data to construct the desired Business Key?

I finally figured it out.

#{S(execution.getVariable(“objects”)).elements().get(0).prop(“id”)}

@ddrouin just to clarify: your Objects variable is being stored as what Type of variable ? A String? If a string, note that Strings have a 4000 character limit. (hard limit imposed throughout the system). So if you are storing JSON that can increase in size, you should store it as a SPIN variable as soon as your receive it and not store the String version.

You can then modify your Expression to: ${objects.elements().get(0).prop(“id”)}

1 Like

Thanks for the info @StephenOTT - we are dealing with Camunda as a black box running on an EC2 instance so all interaction is through the API. I’m not seeing how I can store this as a SPIN variable in that case. Is there a way? Good to know about the character limitation for strings.

You can create a spin Json variable with the rest api. When you save a variable through the rest api, use type “Json”. Search the forum for this as there are many different examples and disucussions.

Thanks @StephenOTT I’ll have a look and give that a try.