{{announcement.body}}
{{announcement.title}}

DataWeave 2.0 — Difference Between Map and MapObject

DZone 's Guide to

DataWeave 2.0 — Difference Between Map and MapObject

This article is not only for beginners but also for many of them who are still confused about what is map and mapObject and when do we need to use them.

· Integration Zone ·
Free Resource

This article is not only for beginners but also for many of them who are still confused about what is map and mapObject and when do we need to use them.

Before going into when to use map and mapObject, let's first know how can we identify whether the data is an Array or an Object!

a) To Identify if the input is an Object or not:

Very simple. Remember an Object starts with { and end with }

Java
 




x
18


 
1
Identification Syntax for Object:
2
 
          
3
{
4
 
          
5
<you can have multiple arrays inside and objects inside again>
6
 
          
7
}
8
 
          
9
Example 1:
10
 
          
11
{
12
    "ID": "1",
13
    "NAME": "Sravan",
14
    "DESIGNATION": "Developer"
15
}
16
 
17
 
          
18
The above example is Single object with fields ID , NAME, Designation



Example 2:

You can have an array inside an Object:

Observe field Designation which has 2 values enclosed in an array

developer, architect

Example 3:

We can have nested Objects inside the main object. When I say the main object that is the { } are the root for the whole message

example 3

You can see that firstName and lastName is the nested object inside Name and the whole message is enclosed within { } which is highlighted in yellow!

a) To Identify if the input is an Array or not:

Very simple. Remember an Array always starts with [ and end with ]

[ ]

Java
 




xxxxxxxxxx
1
24


 
1
Identification Syntax for Array:
2
 
          
3
[
4
 
          
5
<you can have multiple arrays inside and objects inside again>
6
 
          
7
]
8
 
          
9
Example:
10
 
          
11
[
12
    "ID": "1",
13
    "NAME": "Sravan",
14
    "DESIGNATION": "Developer"
15
]
16
 
          
17
The above example is  for  Single Array with key value pairs with fields ID , NAME, Designation
18
 
          
19
or 
20
 
          
21
["Sravan" , "Lingam" , "1","India"]
22
 
          
23
The above example is for  Array with multiple values with no keys
24
 
          




Example 2:

You can have multiple Objects in an Array. we usually call them as Array Of Objects.

ArrayOfObjects

You can see that we have single [ ] which has multiple objects in it

Example 3:

arrays

You can have nested Arrays inside your main array. The main array is highlighted in Yellow and nested arrays are highlighted in Red.

I think now it's pretty much clear what is an Object and what is an array

  • map operator is used for Arrays
  • mapObject is used for Objects
  • You Cannot use mapObject operator for Arrays
  • You Cannot use map operator for objects

Let's Get Started With Map Operator

Let us consider Example 3 from above Array examples.

Observe below snippet

a) A simple $ gives you whole information present inside the Root Array element

Code: Make sure you are using preview option of Transform message to test without running code.

Java
 




xxxxxxxxxx
1
32


 
1
%dw 2.0
2
var a = [
3
  {
4
    "ID": "1",
5
    "NAME": ["SRAVAN","LINGAM"],
6
    "DESIGNATION": "SSE"
7
  },
8
  {
9
    "ID": "2",
10
    "NAME": ["PRADEEP","B"],
11
    "DESIGNATION": "CONSULTANT"
12
  },
13
  {
14
    "ID": "3",
15
    "NAME": ["SANDEEP","Y"],
16
    "DESIGNATION": "SE"
17
  },
18
  {
19
    "ID": "4",
20
    "NAME": ["SAKETH","M"],
21
    "DESIGNATION": "SUPPORT"
22
  },
23
  {
24
    "ID": "5",
25
    "NAME": ["SRINU","V"],
26
    "DESIGNATION": "ARCH"
27
  }
28
]
29
  
30
output application/json
31
---
32
a map  $



b) Access your fields with help of $.<your field name>

<field name>If you can see the above code, we are writing new fields like firstName and lastname by accessing the array values of input "NAME" field by using $.NAME or $.NAME[0]

$: Gives the value of the field

$$: Gives index of the field

You can also use some (key, value) things to access fields but it's one and the same as accessing fields using $ and $$. it's just replacing $ & $$ with key-value.

map operator

In this way, we can use a map operator to process the Array's

Let's Check MapObject Operator

MapObject needs a (key, value ) or (value, key,index).

You can give any kind of naming convention for item and index. make sure you give correct names while using them inside mapping

One more Example:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4

5
{"a":"b","c":"d"} mapObject (value,key,index) -> { (index) : { (value):key} }



Well, this all about the map and mapObject operators.

As I always say. unless and until you code yourself, you can't learn anything!

Topics:
dataweave 2, dataweave functions, integration, mule 4, mulesoft, mulesoft integration, mulesoft tutorial, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}