How to Skip attribute or key ( eg SSN, password - sensitive )while transforming the object using mapObject
Payload:
[
{
"personal_information": {
"first_name": "Emiliano",
"middle_name": "Romoaldo",
"last_name": "Lesende",
"ssn": "001-08-84382"
},
"login_information": {
"username": "3miliano",
"password": "mypassword1234"
}
},
{
"personal_information": {
"first_name": "Mariano",
"middle_name": "Toribio",
"last_name": "de Achaval",
"ssn": "002-05-34738"
},
"login_information": {
"username": "machaval",
"password": "mypassword4321"
}
}
]
Here I have to remove ssn, password key from.
write now I have personal_information, login_information but i hv more attribute on same level
looking for dynamic way : except personal_information, login_information other attributes doesnt have sensitive information.
I have written below dataweave expression but its giving error. Can someone explain what is the error and solution
MY code:
%dw 2.0
output application/json
// I have to remove sesitive fields like ssn, password from payload
---
payload map (value, key)->{
(value mapObject(obj, objKey, index)->
objKey match {
case str if( objKey as String == "personal_information" ) -> ( obj- "ssn")
case str if( objKey as String == "personal_information" ) -> ( obj- "password")
else -> obj
}
}
Hi @Vidyasagar Mundhe ,
Check this script:
%dw 2.4
output application/json skipNullOn="everywhere"
var payload = [
{
"personal_information": {
"first_name": "Emiliano",
"middle_name": "Romoaldo",
"last_name": "Lesende",
"ssn": "001-08-84382"
},
"login_information": {
"username": "3miliano",
"password": "mypassword1234"
}
},
{
"personal_information": {
"first_name": "Mariano",
"middle_name": "Toribio",
"last_name": "de Achaval",
"ssn": "002-05-34738"
},
"login_information": {
"username": "machaval",
"password": "mypassword4321"
}
}
]
var sensitiveFields = ['ssn','password'] map upper($)
fun processValue (value,key) = if(typeOf(value)~=Object) removeSensitiveData(value)
else
(if ((sensitiveFields contains upper(key))) null else value)
fun removeSensitiveData(item) = item mapObject (value,key) -> (key): processValue(value,key)
---
payload map removeSensitiveData($)
Here I'm walking first through the array of elements, and per each element I'm doing the mapObject. Then I'm checking in a recursive way if the value is an object or not. Based on that I'm removing the content of the attribute if the key is in the list sensitiveFields. With the skipNullOn directive I'm removing it from the result.
Be aware that it won't remove object in this code, only values.
Hope it helps
Good luck!
Juan Cruz Basso