0% found this document useful (0 votes)
2 views

Map_and_MapObject

The document explains the use of the Map and MapObject functions in Data Transformation, specifically within Mulesoft versions 3.9 and 4.2. It details how the Map function modifies elements in an array and provides examples of transformations, while also introducing MapObject, which processes both keys and values of an object. The document emphasizes the importance of understanding these functions for effective data manipulation in integrations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Map_and_MapObject

The document explains the use of the Map and MapObject functions in Data Transformation, specifically within Mulesoft versions 3.9 and 4.2. It details how the Map function modifies elements in an array and provides examples of transformations, while also introducing MapObject, which processes both keys and values of an object. The document emphasizes the importance of understanding these functions for effective data manipulation in integrations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

www.devanshtechnologies.com Email: devanshsuryak@gmail.

com
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------

Data Transformation - Map and MapObject


Introduction
We will be working here on Map and MapObject which are key
functions in Data Transformer. (i.e. Transform message connector)

What is map?
map is a function
1. you will use when you want to modify every element in an
array in the same way.
2. In integrations, map is typically used to transform a payload
from the source system to a format expected by the target
system.
3. It can also be used to transform a payload into a more
convenient format for later processing.

For example, Let's assume we have an array of objects, and


each object represents an employee:

[
{
"name": "Joshua Erney",
"job": "Programmer",
"age": 27
},
{
"name": "Mary Smith",
"job": "Data Analyst",
"age": 32
}
]

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------
You can use map to implement any of the following
transformations to the above array, the below scenarios will be
helpful for your implementation:
a) Add a field, "employed" and set it to true for each
employee
b) Add 1 to every employee's age
c) Remove the age field for each employee
d) Get a list of names of all the employees

Conditions/Important things to be noted:


a) we're not modifying the number of elements in the array.
b) whatever we're trying to implement applies to every element
in the array, not just certain elements in the array.

We should always keep in our mind that if those two criteria


hold true, map is a great tool for the job.

How map uses the above two conditions:

The map function takes two arguments:


1) On its left side, it takes an array
2) On its right side it takes a function referred to as a
callback.
When receiving these parameters, the map function will
iterate over every element in the array and apply the
callback function to each element.

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------
The callback needs to be constructed in a very specific way,
namely it must contain at most two parameters or none in certain
situations.

Typically, you will see it used with only one parameter.


The first parameter represents the current value in the array that
is being iterated over. (i.e, arrayObject as $ in the above figure)

For example, if you have an array of objects, the first parameter


will always represent an object ( or $). The second parameter
represents the current index (or $$) of the iteration, so it will
always be an integer.

The body of the callback function describes how each element of


the array will be modified with respect to the input parameters.

Detailed steps of map Function execution, when map is


iterating over the array passed to it, it will "call back" to the
function passed as its right-hand parameter for every step of the
iteration, passing in the parameters it needs.

map in Action
Time to check out how map works.
Let's implement the 1st transformation that we listed in the
previous section, "Add a field 'employed' and set it to true for each
employee."

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------

We can do this one of two ways. Let's do it the most verbose way
first:

%dw 2.0
output application/java
---
payload map (employee) -> {
name: employee.name,
job: employee.job,
age: employee.age,
employed: true
}

In the above example, DW is calling the map function with our


payload, which is an array of objects. The output of the callback
function is also an object.

Notice that our object contains all the same keys as our original
object as well as all the same values, except that in this case we've
added a single field, "employed", and set it to true.

We have added the "employed" additionally in the output.

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------

The output of this DW script would be the following:

[
{
"name": "Joshua Erney",
"job": "Programmer",
"age": 27,
"employed": true"
},
{
"name": "Mary Smith",
"job": "Data Analyst",
"age": 20,
"employed": true
}
]

You might be wondering if we actually needed to be so explicit


about mapping the first three fields, considering that they were
left completely unchanged. As it turns out, we don't; we could
have implemented the same requirement this way instead:

%dw 2.0
output application/java
---
payload map (employee) ->
employee ++ {employed: true}

Which you could describe as "map the payload by adding the


key:value pair of {employed: true} to each object in the array
instead keeping name, job, and age key:value pairs in the payload
and adding an additional key:value pair of {employed: true}."

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------

With DW, function are given the same privileges as classes, objects, and
scalar data types are in Java. We can create them on the fly (like
anonymous classes), store them as variables, and pass them to other
functions.
More Informative: In DW (employee) -> ... is a function
That's because this is an anonymous function, also called a
lambda. When you construct a lambda in DW you specify a
parameters list, for example (employee, index), or (), if there
are no parameters. Then use an arrow, ->, and everything after
that arrow is the body of the function. If you're uncomfortable
with this syntax, it's entirely possible to name your functions
instead and use those just the same.

The Details
Now that you have an understanding of how to use map, let's dig
into how it works. Learning how map works should prove very
beneficial, because if you understand its mechanics you'll also
understand the mechanics of filter, reduce, groupBy, pluck, and
any other function that takes in a collection and a callback used to
process that collection.
input array.

mapObject
Similar to Map, but instead of processing only the values of an
object, it processes both keys and values as a tuple. Also instead of
returning an array with the results of processing these values
through the lambda (object), it returns an object, which consists
of a list of the key:value pairs that result from processing both
key and value of the object through the lambda.
The lambda(object) is invoked with two parameters: key and
the value. If these parameters are not named, the key is defined
by default as $$ and the value as $.

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------

The only difference between Map and MapObject is, map is


applied on the payload and returns the array of object where
object contains key:value pairs whereas mapObject is applied on
the arrayObject returned observed map as one of the parameter
in the function or on single arrayObject and returns key:value
pair.

--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2

You might also like