dataweavemulesoftmule4

How to update specific field value in different types of json request using mule 4?


In my case, I'm updating a field name that contains NAME or Number then i'm updating that field value as "******". for example, {"firstName": "John"} is updated as {"firstName": "******"} using below dataweave code that I tried in below:

%dw 2.0
output application/json
import * from dw::util::Values
import * from dw::core::Types

fun masking(key,val) = if(!isEmpty(val) and (sizeOf(val)>0) and ((key contains "NAME") or (key contains "NUMBER"))) ("******") else val

fun maskMessage(keyname,value) = 
if (typeOf(value) as String == "Object") 
(value mapObject { ($$) : maskMessage($$,$) }) 
else if (typeOf(value) as String == "Array") 
(value map { ($$) : maskMessage($$,$) }) 
else if (typeOf(value) as String == "String")  masking(upper(keyname as String), value)
else value
---
maskMessage("",payload)

But this code giving problem for other fields adding 0 or 1 as default root node.

Sample Request:

{
    "smrSysten": {
        "smrDetails": {
            "smrName": "DTI01922",
            "status": "Active"
        },
        "ownerDetails": {
            "firstName": "John Mike",
            "languagePreferences": [
                "English",
                "Russian"
            ]
        },
        "secondOwnerDetails": [
            {
                "NameProvided": "Will Jack",
                "languagePreferences": [
                    "English",
                    "Spanish"
                ]
            }
        ]
    }
}

Expected Output:

{
    "smrSysten": {
        "smrDetails": {
            "smrName": "******",
            "status": "Active"
        },
        "ownerDetails": {
            "firstName": "******",
            "languagePreferences": [
                "English",
                "Russian"
            ]
        },
        "secondOwnerDetails": [
            {
                "NameProvided": "******",
                "languagePreferences": [
                    "English",
                    "Spanish"
                ]
            }
        ]
    }
}

where ever place name field occur then the value of that field should be updated as "******". other field values should remains same. The request may be an array or object, this name field will comes in under any section. my dataweave code is working but it adding some interger as node.

Expert, please help me solve this logic.

Thanks in advance.


Solution

  • The error is in your script for the case when using map() on the array, it is mapping each item of the array to an object, probably copied from the mapObject() above. That is creating an object with the item index as the key. Simple replacing it by (value map maskMessage($$,$)) fixes that.

    Having said that the script would be more readable using pattern matching instead of nested ifs. Note that you don't need to compare the type names as strings, you can use the 'is' operator with the actual types. Also the imports are not used. Comparing sizeOf(val)>0 is redundant if you already checking !isEmpty(val).

    %dw 2.0
    output application/json
    
    fun masking(key,val) = if(!isEmpty(val) and ((key contains "NAME") or (key contains "NUMBER"))) "******" else val
    
    fun maskMessage(keyname,value) = 
        value match {
            case is Object -> value mapObject { ($$) : maskMessage($$,$)}
            case is Array -> value map maskMessage($$,$)
            case is String -> masking(upper(keyname as String), value)
            else -> value
        }
    ---
    maskMessage("",payload)
    

    Output:

    {
      "smrSysten": {
        "smrDetails": {
          "smrName": "******",
          "status": "Active"
        },
        "ownerDetails": {
          "firstName": "******",
          "languagePreferences": [
            "English",
            "Russian"
          ]
        },
        "secondOwnerDetails": [
          {
            "NameProvided": "******",
            "languagePreferences": [
              "English",
              "Spanish"
            ]
          }
        ]
      }
    }