Search code examples
muledataweaveanypoint-studiomule-esb

Mule esb 3.8 how to add variable into payload?


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.


Solution

  • 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>