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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Using OKTA as Client Provider in Mulesoft
  • MuleSoft DataWeave Practice: Prime Number Code
  • DataWeave Interview Question: Concatenate Elements of an Array

Trending

  • Unleashing the Power of Redis for Vector Database Applications
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Handling “Element Is Not Clickable at Point” Exception in Selenium
  • Microservices Design Patterns for Highly Resilient Architecture
  1. DZone
  2. Coding
  3. Languages
  4. DataWeave Functions: Learn From Basics and Simplicity (Dev’s Choice)

DataWeave Functions: Learn From Basics and Simplicity (Dev’s Choice)

In this article, learn some basic and useful functions of DataWeave 2.0 (Developer's choice) in MuleSoft for integrations.

By 
Nitish Jain user avatar
Nitish Jain
·
May. 31, 24 · Tutorial
Like (1)
Save
Tweet
Share
1.4K Views

Join the DZone community and get the full member experience.

Join For Free

Hi Muleys!

In this post, we will be learning about basic and useful functions of DataWeave 2.0 with quick examples.

The list of functions used in the article below is selected out from the list of huge functions available in DataWeave(Developer's choice):

  1. Join (join)
  2. Left Join (leftJoin)
  3. Outer Join (outerJoin)
  4. Nested join with map operator
  5. Update as Function
  6. Update as Operator
  7. Max By (maxBy)
  8. Min By (minBy)
  9. Filtering an array (filter)
  10. Map an array (map)
  11. DistinctBy an array (distinctBy)
  12. GroupBy an array (groupBy)
  13. Reduce an array (reduce)
  14. Flatten an array (flatten)

We may or may not have used the DataWeave function in our daily integrations. Let's see the examples below for each function.

1. Join

  • The join function behaves similarly to a SQL database JOIN.
  • The join function combines elements of two arrays by matching two ID criteria for the same index in both arrays.
  • The left and right arrays must be arrays of objects.
  • Importing core::Arrays function is required.
  • Ignores unmatched objects

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
//join(a, b,  (emp) -> emp."Billing Country", (loc)-> loc."Billing Country")
//join(a,b, (a)-> a."Billing Country", (b)-> b."Billing Country")
join(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")


Output:

JSON
 
[
  {
    "l": {
      "Name": "Ram",
      "Billing City": "BLR",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 1000
    },
    "r": {
      "Name": "Shyam",
      "Billing City": "HYD",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 3000
    }
  }
]



Join output

2. Left Join

  • All the joined objects are returned.
  • Importing core::Arrays function is required.
  • Any unmatched left elements are also added.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
leftJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")


Output:

JSON
 
[
  {
    "l": {
      "Name": "Ram",
      "Billing City": "BLR",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 1000
    },
    "r": {
      "Name": "Shyam",
      "Billing City": "HYD",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 3000
    }
  },
  {
    "l": {
      "Name": "Max",
      "Billing City": "NY",
      "Billing Country": "USA",
      "Message": "Hello world!!",
      "Type": "Account",
      "Allowance": 2000
    }
  }
]


LeftJoin output

3. Outer Join

  • All the joined objects are returned.
  • Importing the core::Arrays function is required.
  • Any unmatched left element or right elements are also added.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
outerJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")


Output:

JSON
 
[
  {
    "l": {
      "Name": "Ram",
      "Billing City": "BLR",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 1000
    },
    "r": {
      "Name": "Shyam",
      "Billing City": "HYD",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 3000
    }
  },
  {
    "l": {
      "Name": "Max",
      "Billing City": "NY",
      "Billing Country": "USA",
      "Message": "Hello world!!",
      "Type": "Account",
      "Allowance": 2000
    }
  }
]


Outer Join output

4. Nested Join With Map Operator

  • Use the map function to iterate over each joined object.
  • Importing the core::Arrays function is required.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City":"BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City":"HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
(join(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country"))
map {
    "info": $.l ++ $.r - "Billing Country"
}


Output:

JSON
 
[
  {
    "info": {
      "Name": "Ram",
      "Billing City": "BLR",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 1000,
      "Name": "Shyam",
      "Billing City": "HYD",
      "Message": "Hello world!",
      "Type": "Account",
      "Allowance": 3000
    }
  }
]


Nested Join With Map Operator output

5. Update as Function

For Fieldname

  • This update function updates a field in an object with the specified string value.
  • The function returns a new object with the specified field and value.
  • Introduced in DataWeave version 2.2.2
  • Importing the util::Values function is required.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
c update "element" with "abc"  //string


Output:

JSON
 
{
  "element": "abc"
}


For Fieldname output

For Index

  • Updates an array index with the specified value
  • This update function returns a new array that changes the value of the specified index.
  • Introduced in DataWeave version 2.2.2
  • Importing the util::Values function is required.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
d update 2 with 5  //index


Output:

JSON
 
[
  1,
  true,
  5,
  3,
  false
]


For Index output

6. Update as Operator

  • This new update operator will update a specific field value with a new value given.
  • This feature adds an easy way to update single values in nested data structures without requiring to understand functional recursion.
  • No extra dw libraries are required.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
c update {
    case element at .element -> if (element == a) "Max" else "Mule"
}


Output:

JSON
 
{
  "element": "Max"
}


Update as Operator output

7. Max By

  • Iterates over an array and returns the highest value of comparable elements from it.
  • The items must be of the same type. maxBy throws an error if they are not, and the function returns null if the array is empty.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}

---
a maxBy $.Allowance


Output:

JSON
 
{
  "Name": "Max",
  "Billing City": "NY",
  "Billing Country": "USA",
  "Message": "Hello world!!",
  "Type": "Account",
  "Allowance": 2000
}


Max By output

8. Min By

  • Iterates over an array to return the lowest value of comparable elements from it.
  • The items need to be of the same type. minBy returns an error if they are not, and it returns null when the array is empty.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}

---
a minBy $.Allowance


Output:

JSON
 
{
  "Name": "Ram",
  "Billing City": "BLR",
  "Billing Country": "India",
  "Message": "Hello world!",
  "Type": "Account",
  "Allowance": 1000
}



Min By output

Input payload (common for all functions below)

JSON
 
[{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},
{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000},
{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000},
{"Name": "John","Billing City": "FL","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 4000}]


9. Filtering an Array (filter)

To filter the data based on the condition.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
payload filter ((item, index) -> 
item."Billing Country" == "India"
)


Output:

JSON
 
[
  {
    "Name": "Ram",
    "Billing City": "BLR",
    "Billing Country": "India",
    "Message": "Hello world!",
    "Type": "Account"
  },
  {
    "Name": "Shyam",
    "Billing City": "HYD",
    "Billing Country": "India",
    "Message": "Hello world!",
    "Type": "Account"
  }
]


Filtering an Array (filter)

10. Map an Array (map)

  • Transforming every item in an array

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
payload map ((item, index) -> 
{"Cities": if (item."Billing Country" == "USA") "USA" else "Others"}
)


Output: 

JSON
 
[
  {
    "Cities": "Others"
  },
  {
    "Cities": "USA"
  },
  {
    "Cities": "Others"
  },
  {
    "Cities": "USA"
  }
]


Map an Array (map) output

11. DistinctBy an Array (distinctBy)

Remove duplicate items from an Array.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
payload distinctBy ((item, index) -> 
item."Billing Country"
)


Output:

JSON
 
[
  {
    "Name": "Ram",
    "Billing City": "BLR",
    "Billing Country": "India",
    "Message": "Hello world!",
    "Type": "Account"
  },
  {
    "Name": "Max",
    "Billing City": "NY",
    "Billing Country": "USA",
    "Message": "Hello world!!",
    "Type": "Account"
  }
]


 DistinctBy an Array (distinctBy) output

12. GroupBy an Array (groupBy)

  • Grouping together items in an array based on some value

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
payload groupBy ((item, index) -> 
item."Billing Country"
)


Output: 

JSON
 
{
  "India": [
    {
      "Name": "Ram",
      "Billing City": "BLR",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account"
    },
    {
      "Name": "Shyam",
      "Billing City": "HYD",
      "Billing Country": "India",
      "Message": "Hello world!",
      "Type": "Account"
    }
  ],
  "USA": [
    {
      "Name": "Max",
      "Billing City": "NY",
      "Billing Country": "USA",
      "Message": "Hello world!!",
      "Type": "Account"
    },
    {
      "Name": "John",
      "Billing City": "FL",
      "Billing Country": "USA",
      "Message": "Hello world!!",
      "Type": "Account"
    }
  ]
}


GroupBy an Array (groupBy) output

13. Reduce an Array (reduce)

  • It can be used to transform an array to any other type.

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
payload."Allowance" reduce ((item, accumulator) -> (item + accumulator))


Output:

Plain Text
 
10000


Reduce an Array (reduce) output

14. Flatten an Array (flatten)

DataWeave Code

Plain Text
 
%dw 2.0
output application/json
---
flatten (payload.Name)


Output:

JSON
 
[
  "Ram",
  "Max",
  "Shyam",
  "John"
]


Flatten an Array (flatten) output

Conclusion

As MuleSoft Developers, we use DataWeave codes almost daily in our integrations. The functions mentioned above of Array and examples could help us achieve our desired outputs/results easily.

Happy learning!

JSON Array data type MuleSoft

Published at DZone with permission of Nitish Jain. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Using OKTA as Client Provider in Mulesoft
  • MuleSoft DataWeave Practice: Prime Number Code
  • DataWeave Interview Question: Concatenate Elements of an Array

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: