News Ticker

DataWeave: the map function

Main point:

  • 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.

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.

Figure 1: The array is transformed by the lambda expression

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.

Figure 2: Output of the map transformation

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.

Figure 3: Use the $ default to represent element

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 refer to using either element and index or the default $ and $$.

payload map (element, index) -> {
  'code' : element.symbol,
  'price' : element.price
}

Figure 5: Transformation of JSON array – complete form

payload map {
  'code' : $.symbol,
  'price' : $.price
}

Figure 6: Transformation of JSON array – using defaults

[
  {
    "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

[
  {
    "code": "aapl",
    "price": "£119.36"
  },
  {
    "code": "crm",
    "price": "£258.58"
  }
]

Figure 9: The output from the map transformation in figure 8

DataWeave functions

If you enjoyed this blog you might be interested in the following posts about DataWeave functions.

4 Trackbacks / Pingbacks

  1. DataWeave: the puck function
  2. DataWeave: the mapObject function
  3. Contrast DataWeave and Java Mapping Operations - Next Event Ideas
  4. Contrast DataWeave and Java mapping operations

Leave a Reply

%d