Map_and_MapObject
Map_and_MapObject
com
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------
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.
[
{
"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
--------------------------------------------------------------------------------------------------------------------------------------
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.
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
}
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.
--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2
www.devanshtechnologies.com Email: [email protected]
Phone/Watsapp: 8688575188
--------------------------------------------------------------------------------------------------------------------------------------
[
{
"name": "Joshua Erney",
"job": "Programmer",
"age": 27,
"employed": true"
},
{
"name": "Mary Smith",
"job": "Data Analyst",
"age": 20,
"employed": true
}
]
%dw 2.0
output application/java
---
payload map (employee) ->
employee ++ {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
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
Map and MapObject in DWL - Mulesoft 3.9 and 4.2