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
0 Comments
Inline Feedbacks
View all comments