Map function in Dataweave

 

Map function in Dataweave is used to iterate over array and output the result in to an array

Here is the below Example from which we want to extract the First name and Last name and append both string by space

Input:

{
	"employee":[
		{
			"FirstName":"John",
			"LastName":"Smith",
			"Email":"[email protected]"
		},
		{
			"FirstName":"Bob",
			"LastName":"Stryf",
			"Email":"[email protected]"
		}
	],
	"Company":{
		"name":"IBM"
	}
}

We can write the below dataweave code to extract the First Name and Last Name using map operator and append using ++

%dw 2.0
application/json
---

{
	"candidatedetails": payload.employee map((item,index) -> {
		"fname":item.FirstName ++ " " ++ item.LastName
	})
}

Result:

{
	"candidatedetails": [
		{
			"fname":"John Smith"
		},
		{
			"fname":"Bob Stryf"
		}
	]
}
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ramya
Ramya
3 years ago

{
fname”:”John Smith”
“fname”:”Bob Stryf”          
}
with out using the key how to make output like welcome john smith bobstryf

Lalit Kumar Kumawat
Lalit Kumar Kumawat
1 year ago
Reply to  Ramya

“Welcome “ ++ (payload pluck $ joinBy ” “)

Sam
Sam
9 months ago
Reply to  Ramya

%dw 2.0
output application/json


payload.employee map (“Welcome ” ++ $.FirstName ++ ” ” ++ $.LastName)

Kumar
Kumar
2 years ago

Data weave interview questions