In this article, we will discuss an example of how to mask sensitive data using Dataweave in Mulesoft. Dataweave has an operator called mask which can achieve data masking. In order to use mask function we have to import Values (dw::util::Values).

Input:

{
"employee": {
"id": "101",
"firstName": "John",
"lastName": "Grisham",
"designation": "Engineer",
"salary": 20000,
"gender": "M",
"email": "101@a.com",
"phone": "1234567890"
}
}

Output:

{
"employee": {
"id": "101",
"firstName": "JOHN",
"lastName": "G***",
"designation": "Engineer",
"salary": "$20,000.00",
"gender": "M",
"email": "***@a.com",
"phone": "(###)-###-7890"
}
}

Below image shows the dataweave code and output.

Below image shows the same logic for array input.


The following code snippet helps to modify individual fields in a object.

Input:

[{
"id": "111",
"name": "Ernie",
"address": "Hulo"
}, {
"id": "112",
"name": "John",
"address": "Huawei",
"email": "b@a.com"
}]

Output:

[{
"id": "111",
"name": "Ernie",
"address": "Hulo",
"email": "a@a.com"
},
{
"id": "112",
"name": "John",
"address": "Huawei",
"email": "b@a.com"
}]

Below image shows the dataweave code to achieve above output using update operator.

Below is the dataweave code.


The below shown dataweave code is a usecase to read nested json object keys dynamically.

Scenario: Keys are in a separate array. Values for these are in nested in the same object.

Input:

"data": {
"schema": "g5uhJxM3EDHB9UQ5rdqfhw",
"payload": {
"BillingAddress": {
"State": "MN",
"Street": "45…

The following dataweave code converts each key value entry in an array of objects to separate object.

You can find the above dataweave code below for you to play with it.


The following dataweave code converts attributes to xml payload with namespace in Anypoint studio.

Here the speciality is to keep the second namespace for the inner elements in the root atribute in the xml payload. Generally dataweave doesn’t allow for two namespaces in the root element.

Please find the code below for your reference.

https://gist.github.com/ravitejagrt/9e4172acca1d1d515ca5978686b385dd


The below dataweave code converts the content of and array to a string using reduce operator.

This logic very useful to convert any kind of data into a string in Dataweave.

Please find the code below for your reference.

https://gist.github.com/ravitejagrt/8068c9b86acf1da163ac963eca0e0d34


The following tables presents basic understanding and insights into the keystore and trustore differences

KeyStore

· Keystore is used to store your credential

· Truststore is used to store others credential

— — — — — — — — — — — — — — — — — — —…


How to remove extra space in json array or object using dataweave in Mule 4.

The following dataweave code trims the content of any kind of payload whether it is Array or Object or String.

Find the datweave code below.

Suggestions or requests for dataweave logics or any PoC can be sent to raviteja.gajarla@gmail.com

Ravi Teja Gajarla

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store