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.


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

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.
- the map DataWeave function
- Constrast DataWeave and Java mapping operations
- the p DataWeave function (application properties)
- the pluck DataWeave function
Leave a Reply