News Ticker

DataWeave: the mapObject function

Main points:

  • mapObject takes an object as an input
  • mapObject outputs an object
  • mapObject transforms each key/value pair in the object

What is the mapObject function used for?

The mapObject function is used to transform the data contained in an object. It does this by iterating over each key/value pair in the object and applying a transformation to each key and value. The result of the transformation is collected together and output as an object.

How is the transformation described?

The transformation that is applied to each key and value is described with a DataWeave script. For example, you can refer to the key, value and index of the object to be transformed in the DataWeave script and apply a transformation such as the upper() function.

How does the mapObject work?

The mapObject function takes two inputs, the object to transform and the transformation script which is presented as a lambda expression. The object is always to the left of the mapObject function and the lambda expression is always on the right of the mapObject function.

Simple example of the mapObject function

In the figures below the mapObject function iterations over the keys and values of the input object (figure 1), and passes each the value and key into the lambda expression on the right of the mapObject function (figure 2). The lambda expression extracts the key, value and index of each key/value pair and organises them into a new object.

Figure 1: The input object payload to the mapObject function
Figure 2: The mapObject function and transformation DataWeave script

The output of the mapObject mapping transformation collates the input objects key, value and index as shown in figure 3.

Figure 3: The output from the mapObject transformation

Use the built in default short cuts

The key, value and index can be replaced by the built in short cuts $$, $ and $$$ respectively. The above example can be sampled as shown in figure 4.

payload mapObject element: { index: $$$, key: $$, value: $ }

Figure 4: Use the built in default to shorten the code

DataWeave functions

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

2 Trackbacks / Pingbacks

  1. DataWeave: the map function
  2. Contrast DataWeave and Java mapping operations

Leave a Reply

Discover more from Digital Transformation and Java Video Training

Subscribe now to keep reading and get access to the full archive.

Continue reading