
The Set Variable transformer is set with value #[ [ first "Max" last "Mule"} ].
What is a valid DataWeave expression to set as the message attribute of the Logger to access the value "Max" from the Mule event?
Show answerHide answer
Correct answer: A vars "customer first"
Explanation
Explanation:The Set Variable component sets a variable named customer to a Map (Object) value:
$$#[{ first: "Max", last: "Mule" }]$$
This means:
vars.customer is equal to {first: "Max", last: "Mule"}
To access the value "Max" from the Logger's message attribute using DataWeave, you need to:
Access the Variable: Use the vars keyword to access the variable scope, followed by the variable name, e.g., vars.customer.
Access the Key: Since the variable value is a Map, you access its key (first) using the Map accessor syntax.
DataWeave 2.0 offers a shorthand syntax for accessing nested keys in maps (and variables/attributes which are treated as maps) using dot notation or space notation (path selectors):
Dot Notation (Standard): vars.customer.first
Space Notation (Shorthand): vars customer first (This is option A without the quotes, but DataWeave often permits omission of quotes for simple string keys)
Since option A. vars "customer first" is the only one that correctly identifies the vars scope, the variable name (customer), and the key (first) in a way that DataWeave understands (treating the space as a path/field selector), it is the correct way to form the expression. The expression is interpreted as: Access the variable scope (vars), then the variable named customer, then the key first.
β Incorrect Answers
B. "customer first": This is just a literal String and would not access the variable value.
C. customer first: This is invalid syntax. DataWeave requires you to specify the scope (vars.) or attributes (attributes.) to access flow variables. If you omit the scope, it defaults to payload, which is incorrect here.
D. vars "customer" "first": This syntax is incorrect. While it uses vars, separating the variable name and the key with quotes like this is not standard DataWeave path selector syntax. The simplest and most direct path selector notation is vars customer first or vars.customer.first. Option A is the closest valid representation of the correct path selector.
π References
For detailed information on accessing flow variables and Map keys in DataWeave 2.0, refer to the official MuleSoft documentation:
DataWeave Variables Access: The documentation confirms that variables are accessed using the vars keyword, typically using dot notation (vars.variableName) or path selectors.
DataWeave Path Selectors: DataWeave path selectors allow for key access using dot notation (map.key) or space notation (map key), making expressions like vars customer first valid for navigating nested structures.



