DataWeave Capabilities: Be taught From Fundamentals – DZone – Uplaza

Hello Muleys!

On this put up, we can be studying about fundamental and helpful features of DataWeave 2.0 with fast examples.

The listing of features used within the article beneath is chosen out from the listing of big features out there in DataWeave(Developer’s alternative):

  1. Be part of (be part of)
  2. Left Be part of (leftJoin)
  3. Outer Be part of (outerJoin)
  4. Nested be part of with map operator
  5. Replace as Perform
  6. Replace 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. Scale back an array (cut back)
  14. Flatten an array (flatten)

We might or might not have used the DataWeave operate in our every day integrations. Let’s have a look at the examples beneath for every operate.

1. Be part of

  • The be part of operate behaves equally to a SQL database JOIN.
  • The be part of operate combines parts of two arrays by matching two ID standards for a similar index in each arrays.
  • The left and proper arrays should be arrays of objects.
  • Importing core::Arrays operate is required.
  • Ignores unmatched objects

DataWeave Code

%dw 2.0
output software/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}]
---
//be part of(a, b,  (emp) -> emp."Billing Country", (loc)-> loc."Billing Country")
//be part of(a,b, (a)-> a."Billing Country", (b)-> b."Billing Country")
be part of(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")

Output:

[
  {
    "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
    }
  }
]

2. Left Be part of

  • All of the joined objects are returned.
  • Importing core::Arrays operate is required.
  • Any unmatched left parts are additionally added.

DataWeave Code

%dw 2.0
output software/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:

[
  {
    "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
    }
  }
]

3. Outer Be part of

  • All of the joined objects are returned.
  • Importing the core::Arrays operate is required.
  • Any unmatched left component or proper parts are additionally added.

DataWeave Code

%dw 2.0
output software/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:

[
  {
    "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
    }
  }
]

4. Nested Be part of With Map Operator

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

DataWeave Code

%dw 2.0
output software/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}]
---
(be part of(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country"))
map {
    "info": $.l ++ $.r - "Billing Country"
}

Output:

[
  {
    "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
    }
  }
]

5. Replace as Perform

For Fieldname

  • This replace operate updates a area in an object with the required string worth.
  • The operate returns a brand new object with the required area and worth.
  • Launched in DataWeave model 2.2.2
  • Importing the util::Values operate is required.

DataWeave Code

%dw 2.0
output software/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}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
c replace "element" with "abc"  //string

Output:

For Index

  • Updates an array index with the required worth
  • This replace operate returns a brand new array that adjustments the worth of the required index.
  • Launched in DataWeave model 2.2.2
  • Importing the util::Values operate is required.

DataWeave Code

%dw 2.0
output software/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}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
d replace 2 with 5  //index

Output:

6. Replace as Operator

  • This new replace operator will replace a selected area worth with a brand new worth given.
  • This function provides a simple strategy to replace single values in nested information constructions with out requiring to grasp practical recursion.
  • No further dw libraries are required.

DataWeave Code

%dw 2.0
output software/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}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
c replace {
    case component at .component -> if (component == a) "Max" else "Mule"
}

Output:

7. Max By

  • Iterates over an array and returns the very best worth of comparable parts from it.
  • The objects should be of the identical kind. maxBy throws an error if they aren’t, and the operate returns null if the array is empty.

DataWeave Code

%dw 2.0
output software/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}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}

---
a maxBy $.Allowance

Output:

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

8. Min By

  • Iterates over an array to return the bottom worth of comparable parts from it.
  • The objects should be of the identical kind. minBy returns an error if they aren’t, and it returns null when the array is empty.

DataWeave Code

%dw 2.0
output software/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}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}

---
a minBy $.Allowance

Output:

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

Enter payload (widespread for all features beneath)

[{"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 information based mostly on the situation.

DataWeave Code

%dw 2.0
output software/json
---
payload filter ((merchandise, index) -> 
merchandise."Billing Country" == "India"
)

Output:

[
  {
    "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"
  }
]

10. Map an Array (map)

  • Remodeling each merchandise in an array

DataWeave Code

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

Output: 

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

11. DistinctBy an Array (distinctBy)

Take away duplicate objects from an Array.

DataWeave Code

%dw 2.0
output software/json
---
payload distinctBy ((merchandise, index) -> 
merchandise."Billing Country"
)

Output:

[
  {
    "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"
  }
]

12. GroupBy an Array (groupBy)

  • Grouping collectively objects in an array based mostly on some worth

DataWeave Code

%dw 2.0
output software/json
---
payload groupBy ((merchandise, index) -> 
merchandise."Billing Country"
)

Output: 

{
  "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"
    }
  ]
}

13. Scale back an Array (cut back)

  • It may be used to rework an array to another kind.

DataWeave Code

%dw 2.0
output software/json
---
payload."Allowance" cut back ((merchandise, accumulator) -> (merchandise + accumulator))

Output:

14. Flatten an Array (flatten)

DataWeave Code

%dw 2.0
output software/json
---
flatten (payload.Title)

Output:

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

Conclusion

As MuleSoft Builders, we use DataWeave codes virtually every day in our integrations. The features talked about above of Array and examples may assist us obtain our desired outputs/outcomes simply.

Comfortable studying!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version