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.
JSON
x
1
[
2
{
3
"id": "1",
4
"firstName": "Tom",
5
"lastName": "Cruise",
6
"age":25,
7
"salary":20000
8
},
9
{
10
"id": "2",
11
"firstName": "Maria",
12
"lastName": "Sharapova",
13
"age":28,
14
"salary":10000
15
},
16
{
17
"id": "3",
18
"firstName": "James",
19
"lastName": "Bond",
20
"age":32,
21
"salary":30000
22
}
23
]
Example 1 - Mask the Age to "***"
We need to mask the age to "***". To do this, we can use the mask function.
JSON
xxxxxxxxxx
1
1
%dw 2.0
2
import * from dw::util::Values
3
output application/json
4
---
5
payload mask field("age") with "***"
Output:
JSON
x
1
[
2
{
3
"id": "1",
4
"firstName": "Tom",
5
"lastName": "Cruise",
6
"age": "***",
7
"salary": 20000
8
},
9
{
10
"id": "2",
11
"firstName": "Maria",
12
"lastName": "Sharapova",
13
"age": "***",
14
"salary": 10000
15
},
16
{
17
"id": "3",
18
"firstName": "James",
19
"lastName": "Bond",
20
"age": "***",
21
"salary": 30000
22
}
23
]
Example 2 - Mask the Age to "***" and Salary to "xxxx"
JSON
xxxxxxxxxx
1
1
%dw 2.0
2
import * from dw::util::Values
3
output application/json
4
---
5
(payload mask field("age") with "***") mask field("salary") with "xxxx"
Output:
JSON
xxxxxxxxxx
1
23
1
[
2
{
3
"id": "1",
4
"firstName": "Tom",
5
"lastName": "Cruise",
6
"age": "***",
7
"salary": "xxxx"
8
},
9
{
10
"id": "2",
11
"firstName": "Maria",
12
"lastName": "Sharapova",
13
"age": "***",
14
"salary": "xxxx"
15
},
16
{
17
"id": "3",
18
"firstName": "James",
19
"lastName": "Bond",
20
"age": "***",
21
"salary": "xxxx"
22
}
23
]
Example 3 - Format Salary to "$#,###.00"
JSON
xxxxxxxxxx
1
1
%dw 2.0
2
import * from dw::util::Values
3
output application/json
4
type Currency= String{format:"\$#,###.00"}
5
---
6
payload mask field("salary") with $ as Currency
JSON
xxxxxxxxxx
1
23
1
[
2
{
3
"id": "1",
4
"firstName": "Tom",
5
"lastName": "Cruise",
6
"age": 25,
7
"salary": "$20,000.00"
8
},
9
{
10
"id": "2",
11
"firstName": "Maria",
12
"lastName": "Sharapova",
13
"age": 28,
14
"salary": "$10,000.00"
15
},
16
{
17
"id": "3",
18
"firstName": "James",
19
"lastName": "Bond",
20
"age": 32,
21
"salary": "$30,000.00"
22
}
23
]
Example 4 - Convert FirstName and LastName to Uppercase
JSON
xxxxxxxxxx
1
1
%dw 2.0
2
import * from dw::util::Values
3
output application/json
4
type Currency= String{format:"\$#,###.00"}
5
---
6
(payload mask field("firstName") with upper($)) mask field("lastName") with upper($)
Output:
JSON
xxxxxxxxxx
1
23
1
[
2
{
3
"id": "1",
4
"firstName": "TOM",
5
"lastName": "CRUISE",
6
"age": 25,
7
"salary": 20000
8
},
9
{
10
"id": "2",
11
"firstName": "MARIA",
12
"lastName": "SHARAPOVA",
13
"age": 28,
14
"salary": 10000
15
},
16
{
17
"id": "3",
18
"firstName": "JAMES",
19
"lastName": "BOND",
20
"age": 32,
21
"salary": 30000
22
}
23
]
Now, you know how to mask data with MuleSoft DataWeave.
MuleSoft
Data (computing)
JSON
Masking (Electronic Health Record)
Opinions expressed by DZone contributors are their own.
Comments