-1

let say I have payload:

{ Name=User1, Age=29 }

and variable:

{ Address=Planet Earth}

I want to have a check if that variable not null then add it into payload. So final result will be:

{ Name=User1, Age=29, Address=Planet Earth }

How? Was try via payload.Put(variable) but that not allow me such.

Gorodeckij Dimitrij
  • 4,209
  • 2
  • 17
  • 21

1 Answers1

2

With DataWeave using a Transform component you can use the expression: payload ++ flowVars.variable

If you don't want or can't use DataWeave then you can use a MEL expression that uses the Java method Map.putAll() of the Map interface. You can not use <set-payload> because it doesn't return a value. Instead you can use the <expression-component> component.

Example:

<expression-component doc:name="Expression"><![CDATA[payload.putAll(flowVars.variable)]]></expression-component>
aled
  • 21,330
  • 3
  • 27
  • 34
  • 1
    I updated the answer with a non DataWeave solution. – aled Jul 14 '22 at 16:43
  • supper that even better. I was try call dw in mel like: dw("payload ++ flowVars.variable") but it don't like it – Gorodeckij Dimitrij Jul 15 '22 at 14:17
  • 1
    I recommend to avoid mixing languages in the same expression. DataWeave hides the complexity of the types and implementation classes. DataWeave 2 in Mule 4 is even more powerful. MEL is a scripting language based in Java syntax instead. – aled Jul 15 '22 at 15:07
  • could you advice how to do same but with linked list? 1st payload kind of same but 2nd is now similar to 1st and need nest it to got: {Name=User1, Age=29, Address=[{Planet = Earth, Continent = Europa, Town = London},{Planet = Earth, Continent = Europa, Town = Dublin}]}? – Gorodeckij Dimitrij Jul 18 '22 at 22:11
  • 1
    Open a new question please. – aled Jul 19 '22 at 02:07