DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java String: A Complete Guide With Examples
  • Exploring C++23: Multidimensional Subscript Operator
  • SmartXML: An Alternative to XPath for Complex XML Files
  • An Introduction to Bloom Filters

Trending

  • Smart Deployment Strategies for Modern Applications
  • Java Backend Development in the Era of Kubernetes and Docker
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes
  1. DZone
  2. Coding
  3. Languages
  4. Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator

Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator

A developer and DZone Core member gives a tutorial on using different operators in order to work with arrays in MuleSoft's Dataweave platform.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Jan. 15, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
42.1K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

The map operator is a function in Dataweave which iterates over the items in an array and outputs them into a new array. It basically accepts input as a list of items in an array and manipulates the items in the array in order to form a new array as an output.

The filter operator iterates over an array and applies an expression that returns matching values.

The mapObject operator is a function in Dataweave which iterates over the object using the mapper that acts on keys, values, or indices of that object. 

The filterObject operator iterates over a list of key-value pairs in an object and applies an expression that returns only matching objects, filtering out the rest from the output.

Let's say we have the array ["James","Anthony","Kevin"], which contains three items and needs the output as index of the item and its value at that particular index.

Java
 




x


 
1
%dw 2.0
2
output application/json
3
var requestBody= ["James","Anthony","Kevin"]
4
---
5
requestBody map {
6
    ($$):$
7
}


In the above example, $$ will give the index of the item and $ will give the actual value at that index. So the output of the above Dataweave code will be as follows:

JSON
 




xxxxxxxxxx
1
11


 
1
[
2
  {
3
    "0": "James"
4
  },
5
  {
6
    "1": "Anthony"
7
  },
8
  {
9
    "2": "Kevin"
10
  }
11
]


We will be going through various examples of the map and mapObject operator.

Example 1

We have an array of items which contains employee details like firstName, lastName, age, and salary and want to concatenate the firstName and lastName in the final output. To achieve this, we will be using the map operator to iterate over each employee object in the array and concatenate the firstName and lastName as shown in the below Dataweave transformation.

Java
 




xxxxxxxxxx
1
27


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody map {
24
    name: $.firstName ++ " "++ $.lastName,
25
    age: $.age,
26
    salary:$.salary
27
}


Output:

JSON
 




xxxxxxxxxx
1
17


 
1
[
2
  {
3
    "name": "James Peter",
4
    "age": 30,
5
    "salary": 30000
6
  },
7
  {
8
    "name": "Peter Gonsalves",
9
    "age": 28,
10
    "salary": 60000
11
  },
12
  {
13
    "name": "Anthony Watson",
14
    "age": 27,
15
    "salary": 50000
16
  }
17
]


We can also use lambda expressions with the map operator to achieve the same thing.

JSON
 




xxxxxxxxxx
1
27


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody map(items,index) -> {
24
    name: items.firstName ++ " "++ items.lastName,
25
    age: items.age,
26
    salary:items.salary
27
}


Example 2

We will see how the filter operator can be used with the map operator. We want a list of employees whose salary is greater than $40,000 and to also concatenate firstName and lastName in the final output.

JSON
 




x


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody filter $.salary > 40000 map(items,index) -> {
24
    name: items.firstName ++ " "++ items.lastName,
25
    age: items.age,
26
    salary:items.salary
27
}


Output:

JSON
 




xxxxxxxxxx
1
12


 
1
[
2
  {
3
    "name": "Peter Gonsalves",
4
    "age": 28,
5
    "salary": 60000
6
  },
7
  {
8
    "name": "Anthony Watson",
9
    "age": 27,
10
    "salary": 50000
11
  }
12
]


Example 3

We want to convert all the keys to uppercase in the array of items. We can iterate over the array of items using the map operator and to manipulate the keys in the object we can use the mapObject operator, as this operator can act on the keys, unlike the map operator. 

In the mapObject operator, $ gives the value, $$ gives the key, and $$$ gives the indices.

JSON
 




x





1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody map (items,index) ->
24
(
25
    items mapObject(
26
        (upper($$)):$
27
    )
28
)


Output:

JSON
 




xxxxxxxxxx
1
20


 
1
[
2
  {
3
    "FIRSTNAME": "James",
4
    "LASTNAME": "Peter",
5
    "AGE": 30,
6
    "SALARY": 30000
7
  },
8
  {
9
    "FIRSTNAME": "Peter",
10
    "LASTNAME": "Gonsalves",
11
    "AGE": 28,
12
    "SALARY": 60000
13
  },
14
  {
15
    "FIRSTNAME": "Anthony",
16
    "LASTNAME": "Watson",
17
    "AGE": 27,
18
    "SALARY": 50000
19
  }
20
]


Example 4

We want the list of employees whose salary is greater than $40,000 and we want only age and salary in the final output. We can achieve this using multiple approaches.

JSON
 




x


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody filter $.salary > 40000 map (items,index) ->
24
{
25
    age: items.age,
26
    salary: items.salary
27
}


Here's another way to use the filterObject operator:

JSON
 




x


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody filter $.salary > 40000 map (items,index) ->
24
(
25
    items filterObject $$$ > 1
26
)


Output:

JSON
 




xxxxxxxxxx
1
10


 
1
[
2
  {
3
    "age": 28,
4
    "salary": 60000
5
  },
6
  {
7
    "age": 27,
8
    "salary": 50000
9
  }
10
]


Example 5

We want to add the "STATUS":"Active" key value pair between lastName and age in each employee item in the array and also display those employees whose salary is less than $40,000 with all keys uppercased.

JSON
 




x





1
%dw 2.0
2
output application/json
3
var requestBody= 
4
[{
5
    "firstName": "James",
6
    "lastName": "Peter",
7
    "age":30,
8
    "salary":30000
9
},
10
{
11
    "firstName": "Peter",
12
    "lastName": "Gonsalves",
13
    "age":28,
14
    "salary":60000
15
},
16
{
17
    "firstName": "Anthony",
18
    "lastName": "Watson",
19
    "age":27,
20
    "salary":50000
21
}]
22
---
23
requestBody filter $.salary < 40000 map (items,index) ->(
24
    items mapObject (
25
        if($$ as String == "lastName")
26
        {
27
            (upper($$)):$,
28
            "STATUS":"Active"
29
        }
30
        else
31
        {
32
            (upper($$)):$
33
        }
34
        
35
    )
36
)


Output:

JSON
 




xxxxxxxxxx
1


 
1
[
2
  {
3
    "FIRSTNAME": "James",
4
    "LASTNAME": "Peter",
5
    "STATUS": "Active",
6
    "AGE": 30,
7
    "SALARY": 30000
8
  }
9
]


Difference Between Map and MapOperator Objects

  • Map basically works on the array and the output will always be an array, whereas MapObject works on single items or objects and the output will always be an object.
  • Map provides the value and index: value can be accessed using $, index can be accessed using $$ 
  • MapObject provides the value, key, and index: the value can be accessed using $, the key can be accessed using $$, and the index can be accessed using $$$.
  • Filter can be used with the Map operator or an array of items whereas FilterObject can be used with the MapObject operator or object.

DataWeave 2.0 Map Operator and Filter With MuleSoft


DataWeave Transformation (Map and MapObject Operator) With MuleSoft

DataWeave Transformation (Filter and FilterObject) With MuleSoft

Now you know how to use Map, Filter, MapObject, and FilterObject with MuleSoft Dataweave transformations.

Operator (extension) Object (computer science) Filter (software) Data structure JSON

Opinions expressed by DZone contributors are their own.

Related

  • Java String: A Complete Guide With Examples
  • Exploring C++23: Multidimensional Subscript Operator
  • SmartXML: An Alternative to XPath for Complex XML Files
  • An Introduction to Bloom Filters

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook