DataWeave: The map Function Explained
The DataWeave map function is used to transform data contained in an array. It does this by iterating over the elements in the array and applying a transformation to each element.
Join the DZone community and get the full member experience.Join For Free
mapfunction transforms data
- If iterates over the elements in an array
- It applies a transformation to each element
- Applies only to array input
- It can only output an array
What Is the map Function Used For?
map function is used to transform the data contained in an array. It does this by iterating over the elements in the array and applying a transformation to each element. The result of the transformation is collected together and output as an array of transformed elements. If you are familiar with Java 8 functional programming approach, a comparison can be made with the the
map() method of the
Stream class, find out more in my article Contrast DataWeave and Java mapping operations.
How Is the Transformation Described?
The transformation that is applied to each element is described with DataWeave. For example, to change a text value in the source array use the
upper() built in function.
How Does the map Function Work?
map function takes two inputs, the array to transform and the transformation script which is presented as a lambda expression. The array is always to the left of the
map function and the lambda expression is always on the right of the
Simple Example of the map Function
In figure 1 the
map function iterations over the array, element by element, and passes each element into the lambda expression on the right of the
map function. The lambda expression passes the element in to the
upper() function which transforms the element to uppercase.
map function iterates over every element in the array and applies the transformation lambda to each element. The output of each transformation is collected together into an array. Figure 2 shows the output.
The map Function Defaults
The lambda function can be simplified by using the built in defaults for
index, the single dollar
$ and double
$$ respectively. The
map example in figure 1 would be transformed as shown in figure 3.
Transform a JSON Array With the map Function
map function can be used to transform an array of JSON objects. The transformation expression is applied to each object in the array. The dollar (
element) refers to the entire object so that a field of that object can be referenced using the single selector notion. Consider the array of JSON objects in the figure 4.
Figure 4: An array of JSON objects
Each object has two fields
price, which have associated values. They can be referred to by using either
index or the default
Figure 5: Transformation of JSON array - complete form
Figure 6: Transformation of JSON array - using defaults
Figure 7: Output of figure 5 and 6
Advanced Example of the map Function
A more complex example would transform elements' values, such as shown in figure 8.
Figure 8: Transform each element in the JSON array
Figure 9: The output from the map transformation in figure 8
DataWeave mapObject Function
The DataWeave mapObject function operates on an Object consisting of key and value pair just as the map function does over an array. Each key and value pair is transformed by the given transformation script and the resulting transformation is output to an object.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.