Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Mulesoft DataWeave is a simple powerful tool to transform data inside a flow. Numerous core operators and functions are already present to perform various operations such as capitalize, camelize, upper, and lower.

For string operations, there are no core functions present to resolve a wildcard. I hope the DataWeave(DWL 1.0) function below helps you to perform a requirement where you want to resolve a wildcard using a set of parameters.

Mule4 DataWeave Exercise: map, reduce, and pluck

Using the DataWeave exercise below, I'll demonstrate how to use the reduce, map, valuesOf, splitBy functions, format, and ++ operator.

Given Input:

JSON
 
[
    {
        "empId": "ex12345",
        "fullName": "Peter parker",
        "street": "Second Ave 46",
        "city": "San Francisco",
        "state": "veniam",
        "postal": "sit",
        "country": "Mexico",
        "dept": "hr",
        "joinedDate": "2021-12-22",
        "miles": 68
    },
    {
        "empId": "ex54321",
        "fullName": "Joseph charles",
        "street": "First Ave 47",
        "city": "Bangalore",
        "state": "Karnataka",
        "postal": "560100",
        "country": "India",
        "dept": "finance",
        "joinedDate": "2021-11-15",
        "miles": 49
    }
]

Desired Output:

JSON
 
[
  {
    "firstName": "Peter",
    "lastName": "parker",
    "AddrWithValuesOf": "San Francisco,veniam,sit,Mexico",
    "AddrWithPluck": "San Francisco,veniam,sit,Mexico",
    "miles": 68.00,
    "DateofJoin": "22-Dec-2021"
  },
  {
    "firstName": "Joseph",
    "lastName": "charles",
    "AddrWithValuesOf": "Bangalore,Karnataka,560100,India",
    "AddrWithPluck": "Bangalore,Karnataka,560100,India",
    "miles": 49.00,
    "DateofJoin": "15-Nov-2021"
  }
]

We can accomplish the desired output by writing the following DataWeave.

Dataweave 2.4.0 Dates Module Functions

DataWeave is a programming language designed for transforming data. It is the primary language of MuleSoft for data transformation and an expression language for components and connectors configuration.

Mulesoft released Dataweave 2.4.0 for Mule Version 4.4. The 2.4.0 version of DataWeave introduced many new features. In this post, we will see one of the newly introduced DataWeave modules.

Mulesoft Dataweave: filter vs takeWhile

filter Vs takeWhile

Both of these functions can be used for selecting elements from the array for a given condition, The difference is "filter" returns all the element which matches condition but 'takeWhile' returns all the elements till the first failure. 

JSON
 
input :
[
    {
        "id": "100",
        "name": "Dataweave by"
    },
    {
        "id": "100",
        "name": "Arpan"
    },
    {
        "id": "200",
        "name": "Kumar"
    },
    {
        "id": "100",
        "name": "Sharma"
    }
]


=========dwl=============
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
// use 'takeWhile' when you wants to select the element till first failure of given condition.
"takeWhile" : payload takeWhile ((item) -> item.id == "100" ) ,
//use 'filter'  when you wants to select all the element for given condition.
"filter": payload filter ((item, index) -> item.id == "100")
}
=========output=============


{
  "takeWhile": [
    {
      "id": "100",
      "name": "Dataweave by"
    },
    {
      "id": "100",
      "name": "Arpan"
    }
  ],
  "filter": [
    {
      "id": "100",
      "name": "Dataweave by"
    },
    {
      "id": "100",
      "name": "Arpan"
    },
    {
      "id": "100",
      "name": "Sharma"
    }
  ]
}


Implement a Counter in Dataweave 2.x and Above

Implement a Counter in Dataweave

These days, I see quite a few posts that require using Counter in DataWeave. The problem faced is that users require a Sequence Number that is consistent throughout the different sections of data. Consider the below JSON:

JSON
xxxxxxxxxx
1
17
 
1
[{
2
"Item" : "ItemXYZ",
3
  "SequenceNo" : 1,
4
  "Parts": [
5
{ "Part" : "A1", "SequenceNo" : 2 }, { "Part" : "B1", "SequenceNo" : 3 },
6
{ "Part" : "A2", "SequenceNo" : 4 }, { "Part" : "B2", "SequenceNo" : 5 }
7
  ]
8
},
9
 {
10
"Item" : "ItemRTY",
11
  "SequenceNo" : 6,
12
  "Parts": [
13
{ "Part" : "A1", "SequenceNo" : 7 }, { "Part" : "B1", "SequenceNo" : 8 },
14
{ "Part" : "A2", "SequenceNo" : 9 }, { "Part" : "B2", "SequenceNo" : 10 }
15
  ]
16
}
17
]