DataWeave 2.0 provides two powerful helpers – map for arrays and mapObject for objects – designed to reshape and transform every entry in a collection. By choosing the right one, you keep your code concise, efficient, and expressive.
When to use
mapapplies a transformation function to each element of an array, returning a new array of the same length.mapObjectapplies a transformation to each key – value pair of an object, returning a new object built from the single – entry objects your function produces.
map in Action
Use map whenever you need to compute a new value for every element. Your function receives (value, index) and returns the transformed value.
Example: Convert a list of Celsius readings to Fahrenheit:
Here, map loops through the array, applies the formula (c * 9/5) + 32 to each temperature, and assembles the results into a new array.
mapObject in Action
Use mapObject when you need to rename keys or reshape values within an object. Your function receives (value, key, index) and must return a one-property object; DataWeave merges these into the output.
Example: Prefix each product key with "prod-" and format its price as a string:
This approach lets you stay in “object space” without converting to an array or back.
Best practices for transformers
- Precompute heavy logic (like regexes or lookups) outside the helper to avoid repeated work.
- Name your parameters clearly: use meaningful names like
(price, product)instead of generic(v, k). - Leverage
defaultto guard against missing values before transforming.
Conclusion
By using map for arrays and mapObject for objects, you can cleanly express transformations without boilerplate loops or conversions. Precompute shared logic, choose clear parameter names, and leverage chaining to produce concise, maintainable code. Master these transformers, and you’ll handle virtually any data-reshaping scenario with ease.