DataWeave 2.0 - Part 1 | Difference between map and mapObject
Sravan Lingam

DataWeave 2.0 - Part 1 | Difference between map and mapObject

Hey Muleys!

Am gonna start over DataWeave in parallel . I will be delivering the articles in parts , as we have many things to coverup

This article is not only for the beginners but also for many of them who are still confused 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 input is an Object or not:

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

Identification Syntax for Object:

{

<you can have multiple arrays inside and objects inside again>

}

Example 1:

{
    "ID": "1",
    "NAME": "Sravan",
    "DESIGNATION": "Developer"
}
 

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

No alt text provided for this image

Example 3:

We can have nested Object inside mainObject. When i say mainObject that is the { } are th root for whole message

No alt text provided for this image

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


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

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

[ ]

Identification Syntax for Array:

[

<you can have multiple arrays inside and objects inside again>

]

Example:

[
    "ID": "1",
    "NAME": "Sravan",
    "DESIGNATION": "Developer"
]

The above example is  for  Single Array with key value pairs with fields ID , NAME, Designation

or 

["Sravan" , "Lingam" , "1","India"]

The above example is for  Array with multiple values with no keys


Example 2:

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

No alt text provided for this image

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








Example 3:


No alt text provided for this image

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







I think now its 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

No alt text provided for this image

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

%dw 2.0
var a = [
  {
    "ID": "1",
    "NAME": ["SRAVAN","LINGAM"],
    "DESIGNATION": "SSE"
  },
  {
    "ID": "2",
    "NAME": ["PRADEEP","B"],
    "DESIGNATION": "CONSULTANT"
  },
  {
    "ID": "3",
    "NAME": ["SANDEEP","Y"],
    "DESIGNATION": "SE"
  },
  {
    "ID": "4",
    "NAME": ["SAKETH","M"],
    "DESIGNATION": "SUPPORT"
  },
  {
    "ID": "5",
    "NAME": ["SRINU","V"],
    "DESIGNATION": "ARCH"
  }
]
  
output application/json
---
a map  $

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

No alt text provided for this image

if you can see 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

No alt text provided for this image

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

No alt text provided for this image

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

Let's check mapObject Operator:

mapObject needs a (key ,value ) or (value,key,index)

No alt text provided for this image

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

One more Example:

%dw 2.0
output application/json
---


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


Well this all all about map and mapObject operators.

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

Happy Learning!

Yours Sravan Lingam







Kshitij Gadwe

Mulesoft Developer

4 年

Thanks Sravan for this article. Keep posting more.

Anusha Nalluri

Technical Consultant at Advance Solutions

5 年

Very Simple and Crisp explanation.Please keep posting...

Kishore Podili

Integration Architect | Mulesoft | API Management Integration SME

5 年

good article

Goka Prashanth

Senior Analyst Technology L1 at Publicis Sapient

5 年

Gone through your article and you explained in a simple way to understand easily.

Sai Satyamayee Gochhayat

Certified Mulesoft Developer|JAVA|J2EE|SpringBoot|Microservices

5 年

Well explained with so much clarification

要查看或添加评论,请登录

Sravan Lingam的更多文章

社区洞察

其他会员也浏览了