Sushma
Sushma

Reputation: 121

How to mask values in complex json structure using dataweave?

I want to mask the element in the json. descID element in the below json should be masked. Could you please suggest.

{
    "status": "ok",
    "statusCode": "19x9s011",
    "statusDescription": "Service: XYZ IOP ; country: india ; Locale:en-US ; SourceId:KOP; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f899s2c2b; Description: The NMK profile call was successful.",
    "details": {
        "descID": "11840000000012698",
        "Code": "XX",
        "languageCode": "en",
        "profile": {
            "base": {
                "username": "abc",
                "firstName": "xc",
                "middleName": "test",
                "lastName": "123",
                "shortName": "xc",
                "displayName": "D",
                "suffix": "T",
                "prefix": "E"
            }
        }
    }
}

Upvotes: 1

Views: 1876

Answers (3)

jsilverdev
jsilverdev

Reputation: 21

You can use this function for one nested item:

    %dw 2.0
    import update from dw::util::Values
    output application/json
    ---
    payload update ["details", "descID"] with "***"

But if you need mask more values you can use

    %dw 2.0
    import update from dw::util::Values
    
    fun maskProperties(obj : Object, props: Array<String>) : Object = (
        if(isEmpty(obj)) obj
        else (
            props reduce (
                (prop, acc = obj) -> acc update (prop splitBy ".") with (
                    if (isEmpty($)) $
                    else "***"
                )
            )
        ) as Object
    )
    ---
    maskProperties(payload, ["details.descID","details.Code"])

This function will iterate over the props and accumulate the changes in the acc attribute and finally return the updated value

Upvotes: 0

user3366906
user3366906

Reputation: 149

1.Convert payload to object

<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>

2.Overwrite descID in payload

<expression-component doc:name="Expression"><![CDATA[payload.details.descID=flowVars.maskedvalue;]]></expression-component>

3.Convert back to json

<json:object-to-json-transformer doc:name="Object to JSON"/>

Upvotes: 0

Chad Gorshing
Chad Gorshing

Reputation: 3118

I might use something like to mask the data:

%dw 1.0
%input payload application/json
%output application/json


%var keyToEncrypt = ['descID']

%function encrypt(val) "*****"

%function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0


%function maskSensitiveData(value) value mapObject ({
($$) : maskSensitiveData($) when $ is :object otherwise $
} unless needsEncrypt($$ as :string) otherwise {
($$) : encrypt($)
})

---

maskSensitiveData(payload)

This is taken verbatim from https://blogs.mulesoft.com/dev/training-dev/encrypt-specific-xml-tags-with-the-power-of-dataweave/

If you need to remove the field altogether, then I might use something like:

%dw 1.0
%input payload application/json
%output application/json


%var keyToEncrypt = ['descID']

%function encrypt(val) "*****"

%function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0


%function maskSensitiveData(value) value mapObject ({
($$) : maskSensitiveData($) when $ is :object otherwise $
} unless needsEncrypt($$ as :string) otherwise {})

---

maskSensitiveData(payload)

Upvotes: 1

Related Questions