Masking Data With MuleSoft DataWeave
Learn how to keep data private with DataWeave.
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will see various examples of how we can mask data using MuleSoft DataWeave. MuleSoft DataWeave has a helper function, mask, in the DW Utils Values module.
We will be using the payload below to understand the mask function.
x
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age":25,
"salary":20000
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age":28,
"salary":10000
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age":32,
"salary":30000
}
]
Example 1 - Mask the Age to "***"
We need to mask the age to "***". To do this, we can use the mask function.
xxxxxxxxxx
%dw 2.0
import * from dw::util::Values
output application/json
---
payload mask field("age") with "***"
Output:
x
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age": "***",
"salary": 20000
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age": "***",
"salary": 10000
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age": "***",
"salary": 30000
}
]
Example 2 - Mask the Age to "***" and Salary to "xxxx"
xxxxxxxxxx
%dw 2.0
import * from dw::util::Values
output application/json
---
(payload mask field("age") with "***") mask field("salary") with "xxxx"
Output:
xxxxxxxxxx
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age": "***",
"salary": "xxxx"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age": "***",
"salary": "xxxx"
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age": "***",
"salary": "xxxx"
}
]
Example 3 - Format Salary to "$#,###.00"
xxxxxxxxxx
%dw 2.0
import * from dw::util::Values
output application/json
type Currency= String{format:"\$#,###.00"}
---
payload mask field("salary") with $ as Currency
xxxxxxxxxx
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age": 25,
"salary": "$20,000.00"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age": 28,
"salary": "$10,000.00"
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age": 32,
"salary": "$30,000.00"
}
]
Example 4 - Convert FirstName and LastName to Uppercase
xxxxxxxxxx
%dw 2.0
import * from dw::util::Values
output application/json
type Currency= String{format:"\$#,###.00"}
---
(payload mask field("firstName") with upper($)) mask field("lastName") with upper($)
Output:
xxxxxxxxxx
[
{
"id": "1",
"firstName": "TOM",
"lastName": "CRUISE",
"age": 25,
"salary": 20000
},
{
"id": "2",
"firstName": "MARIA",
"lastName": "SHARAPOVA",
"age": 28,
"salary": 10000
},
{
"id": "3",
"firstName": "JAMES",
"lastName": "BOND",
"age": 32,
"salary": 30000
}
]
Now, you know how to mask data with MuleSoft DataWeave.
Opinions expressed by DZone contributors are their own.
Comments