Substring on JSON string element

I need to do something very simple but can’t get it to work. I have a process variable called “customer” that contains a JSON string, but it is a string data type, not json. I need to extract customer.account and take the first three characters. I’ve tried everything I can think of. I just need an activity that will put the substring into a variable “area”. I’m about ready to do it using an external task, but this should be possible in BPMN. Any help is greatly appreciated.

Hi HaIE :wave:
Welcome to the Camunda Forum!

You could use the Camunda Spin library in order to handle the JSON string as proper JSON object or cast it into a proper Java object (depending on what you prefer). This should make it easily possible to access the variables from an inline script.

→ about Spin https://github.com/camunda/camunda-spin
→ Documentation: Camunda Spin Dataformat Reference | docs.camunda.org

Let me know whether this didn’t help you. :slight_smile:
Best,
Thomas

1 Like

Hi Thomas,
Thanks so much for responding. We’ve spent two days on this and we can’t get it to work. This is the last script we tried:

import static org.camunda.spin.Spin.*;
def customer = execution.getVariable(“customer”);
out.println("Script: " + customer.toString());
def customerObj = JSON(customer.toString());
SpinJsonNode account = customerObj.prop(“account”);
execution.setVariable(“Account”, account.stringValue());
execution.setVariable(“Area”, account.stringValue().substring(0,3));

It says SpinJsonNode is undefined, but it came right out of the docs. The println() has the correct string. A script would be appropriate here, but since nothing we tried worked (and we tried at least 20 variations), we just created an external task to do it.
Thanks for your help!
Hal

Oh no, I am sorry to hear that you spend so much time on this topic.
Let me ask you…

  • if you have added the Spin maven dependency to your pom.xml file?
  • how is the customer passed into the process. Via Rest or differently?
  • Could you post the link to the docu where you found this statement?

Great that you used an external task worker for now. They are way more powerful anyway. :slight_smile: