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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  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
Likes (1)
Comment
Save
Tweet
Share
2.7K 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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!