1
votes

I'm trying to map a column name dynamically in Dataweave using the contents of a flow variable. The static version of what I'm trying to achieve looks something like:

payload map ((payload01 , indexOfPayload01) -> {
    COLUMNA: payload01.INPUTA
})

Now the part that I need to be dynamic is INPUTA - I need the A to be derived from a flow variable that is set prior to the Dataweave component. I've tried something like:

payload map ((payload01 , indexOfPayload01) -> {
    COLUMNA: payload01.INPUT#flowVars['varName']
})

But I'm getting a com.mulesoft.weave.grammar.InvalidNamespacePrefixException error.

Is this possible to achieve in Dataweave? Can't seem to find any relevant docs describing how.

3

3 Answers

1
votes

Use it like below. Note the parenthesis around key which is very important because () tells DataWeave to first execute script inside it and then use the result as the value for outside expression.

    payload map ((payload01 , indexOfPayload01) -> {
    COLUMNA: payload01[('INPUT' ++ flowVars.varName)]
})

Let me know that doesn't work.

I see your key name is COLUMNA, you can also append varName to key by using ().

0
votes

Try to access flowVar just like MEL way: #[flowVars.varName], donot prefix with payload01

0
votes

From Dataweave you have to access the flowvars or session vars as given below. flowVars.varName sessionVars.varName

You can use various Dataweave operators to form final field names or value. Understanding Dataweave Operators is crucial to to make best use of Dataweave component. Please go through the link below for a understanding of Dataweave operators.

https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators