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 FreeMain Points:
- The
map
function 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?
The 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?
The 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 map
function.
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.
The 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 element
and 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
The 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.
[
{
"symbol": "AAPL",
"price": 119.36
},
{
"symbol": "CRM",
"price": 258.58
}
]
Figure 4: An array of JSON objects
Each object has two fields symbol
and price
, which have associated values. They can be referred to by using either element
and index
or the default $
and $$
.
xxxxxxxxxx
payload map (element, index) -> {
'code' : element.symbol,
'price' : element.price
}
Figure 5: Transformation of JSON array - complete form
xxxxxxxxxx
payload map {
'code' : $.symbol,
'price' : $.price
}
Figure 6: Transformation of JSON array - using defaults
xxxxxxxxxx
[
{
"symbol": "AAPL",
"price": 119.36
},
{
"symbol": "CRM",
"price": 258.58
}
]
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.
payload map {
'code' : lower($.symbol),
'price' : $.price as String {format: "£#,###.00"}
}
Figure 8: Transform each element in the JSON array
xxxxxxxxxx
[
{
"code": "aapl",
"price": "£119.36"
},
{
"code": "crm",
"price": "£258.58"
}
]
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.
Comments