Is it possible to manipulate a Json object with the Spin API?

I have a script task which creates a Json object.

var data = [
  {
    "value": 1
  }
]

S(JSON.stringify(data))

Now I’ll try to change a property of this object.

var json = execution.getVariable("dataSource");

json.jsonPath("$[0]").element().prop("value", 2)

json

But this doesn’t work. The value is still 1. Is there another possibility to change the value of an existing Json object, and then use this manipulated Json in the rest of the definition?

Hi @christian.kaps,

There’s an inconsistency in the API when you make a JSON path query compared to the other API methods for element access. When you call SpinJsonPathQuery#element, the returned SpinJsonNode wraps a copy of the json node in your json variable. That means, changed properties on that node are not going to be reflected in the json variable. I created a bug ticket to resolve that inconsistency by either documenting it or changing the behavior, see https://app.camunda.com/jira/browse/CAM-6587.

As a workaround, I suggest you navigate to the node not via JSON path but via the Spin API, e.g. json.elements().get(0).prop("value", 2).

Cheers,
Thorben

1 Like