FEEL context merge() function

Hello, Everybody !
Could you help me, please, to understand what’s wrong with my FEEL context merge() function - “Union the given contexts. Returns a new context that includes all entries of the given contexts”.
I have two variables with JSON and I want to merge they together - just add property {“mainExec”:“1”} to “group01” in List.

var01=
{"documentDeadline":"2025-05-28T12:00:00",
"executors":{"execGroups":[
{"execGroup":"group01"},
{"execGroup":"group02"},
{"execGroup":"group03"}
]},
"documentId":"123456"}
var02=
{"executors":{"execGroups":[
{"execGroup":"group01","mainExec":"1"}
]}}

mergedDocument=context merge(var01, var02)

The result is not I expected:

mergedDocument=
{
	"documentDeadline": "2025-05-28T12:00:00",
	"executors": {
		"execGroups": [
			{
				"execGroup": "group01",
				"mainExec": "1"
			}
		]
	},
	"documentId": "123456"
}

Test diagram is here:
FEEL_context_merge_v03.bpmn (5.9 KB)

Maybe it’s possible to add param {“mainExec”:“1”} to “group01” in the List by using context put(context, key, value), but I have problems with this one too :slight_smile:

Ohh… SNEAKY.

In Context functions | Camunda 8 Docs
there’s a small little note…

Since both var1 and var2 have a path of /executors the executors from var2 is overwriting var1.

But I agree with you, that’s not how I would have understood it.
I would have expected the array of /executors/execGroups to be extended, and the key /executors/execGroups/execGroup{group1} to be overwritten.

Playing with it over on the Playground, even if /executors is changed to an array of values, it’s still overwritten, since the key (/executors) is still the same.

Hi @volodya327

The following FEEL expression should generate the expected result

{
  execGroupsItem1Removed: remove(var1.executors.execGroups, 1), // remove {"execGroup":"group01"}
  newExecGroups: insert before(execGroupsItem1Removed, 1, var2.executors.execGroups[1]), // insert {"execGroup":"group01","mainExec":"1"}
  result: {
    documentDeadline: var1.documentDeadline,
    executors: {
      execGroups: newExecGroups
    },
    documentId: var1.documentId
  }
}.result

Thanks for your answers, Gentlemens !
So, is it possible to add item {“mainExec”:“1”} to {"execGroup":"group01"} in var01 variable and get a new one in the List like:
{"execGroup":"group01","mainExec":"1"}
If yes, then how to create var02 or maybe use another function ?

Hi @volodya327

The above expression generates exactly the output you are looking for, which in turn can be assigned to mergedDocument variable.

Thanks, hassang for your solution !
I changed the names of var1 & var2 to var01 & var02 and it works.

But is it possible to make a solution for dynamic List when I don’t know index of item I want to modify ? Just by name.
Or, maybe, it’s possible to calculate the number of index by name ?

Anyway, thank you for this knowledge. I saw some undocumented possibilities for working with context, I can use in the future.

1 Like

Yes, you can find the index of the item using both Projection and Built-in function index of
https://docs.camunda.io/docs/components/modeler/feel/language-guide/feel-context-expressions/#projection
https://docs.camunda.io/docs/components/modeler/feel/builtin-functions/feel-built-in-functions-list/#index-oflist-match

Here is the expression to find the index of execGroup element with the value “group02”
index of(var01.executors.execGroups.execGroup,"group02")

Thank you, hassang for your advises. They always very useful.
The final diagram, I tested the solution in Camunda 8.6 is here.
FEEL_JSON_merge+dynamic_index_Forum_solution.bpmn (8.1 KB)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.