Back to Blog
3 min. read

Common Reusable Dataweave Scripts – Part 1

This article is for Mulesoft Developers looking for generic reusable scripts for the commonly occurring scenarios as there is no direct function for such cases as of today in Mulesoft Dataweave Library. Below cases can be solved in many ways we are representing one of those ways.

Case 1: remove empty script

										%dw 2.0 
output application/json skipNullOn="everywhere" 

/* This Script is for community use 

Why this script ? 

For Json payloads, we can only skip null values fields using skipNullOn="everywhere" however there is not direct reusable property for skipping fields with [] or {} value hence thought came to design and share this generic script 

This script will delete all keys/fields which are having value as [] or {}. You can expand this list in future based on requirement 

Applicable to json payloads currently 

--- coded by vishal.changlani@billennium.com */ 

fun removeEmpties(data) = 
  data match { 
    case is Array -> if ($ == [] or $ == {}) 
      null 
    else 
      ($ map removeEmpties($)) 
    case is Object -> if ($ == {}) 
      null 
    else 
      $ mapObject { 
        ($$): removeEmpties($) 
      } 
    case is String -> $ 
    else -> $ 
  } 

--- 
removeEmpties(payload)
									

Example:

Input Payload:

										[ 
  { 
    "message": "Hello world!" 
  }, 
  { 
    "message": [], 
    "name": "Vishal" 
  }, 
  {}, 
  {} 
] 
									

Expected Output:

										[ 
  { 
    "message": "Hello world!" 
  }, 
  { 
    "name": "Vishal" 
  } 
] 
									

Script for above example:

										%dw 2.0 
output application/json skipNullOn="everywhere" 

fun removeEmpties(data) = 
  data match { 
    case is Array -> if ($ == [] or $ == {}) 
      null 
    else  ($ map removeEmpties($)) 
    case is Object -> if ($ == {}) 
      null 
    else 
      $ mapObject {    ($$): removeEmpties($)} 
    case is String -> $ 
    else -> $ 
  }

---
removeEmpties(payload)
									

Case 2: partialMasking script

										%dw 2.0 
output application/json  skipNullOn="everywhere" 

/* This Script is for community use 
Why this script ? 

Although we have mask() function in Dataweave but it is available in its basic version which can be enhanced to cover other generic cases 

Current limitations :- 
1. We need to write mask on field level basis 
2. It mask entire value, partial masking not available 

Current Implementation :- 
1. No need to write mask at field level, just define keyNames in one var (fields) 
2. Partial Masking is available with option to choose which part of value to be visible and length to be visible 

Just fill out vars -> fields, visibleLocation and visibleLength to use it 

Applicable to json payloads 
--- coded by vishal.changlani@billennium.com */ 

var fields = <<add fields to be masked>>  // eg: ["productId", "name"] 

var visibleLocation = <<text location which should be visible>> // eg: "right" 
// possible options left, right, none 

var visibleLength = <<length of visible text>> // eg: 2 
// length of visible chars 

fun partialMasking(data) = 
  data match { 
    case is Array -> if ($ == [] or $ == {}) 
      null 
    else 
      ($ map partialMasking($)) 
    case is Object -> if ($ == {}) 
      null 
    else 
      $ mapObject if ((fields contains ($$) as String) and !isEmpty($)) 
        { 
          ($$): masking($) 
        }    else 
        { 
          ($$): partialMasking($) 
        } 
    case is String -> data 
    else -> data 
  }
									
										fun masking(value: String) = 
  if (lower(visibleLocation) == "left") 
    (if (sizeOf(value) > visibleLength and visibleLength > 0) 
      (value[0 to visibleLength - 1] ++ (value[visibleLength to -1] replace /\w/ with "*")) 
    else 
      (value[0 to -1] )) 
  else if (lower(visibleLocation) == "right" and visibleLength > 0) 
    (if (sizeOf(value) > visibleLength) 
      (value[0 to sizeOf(value) - visibleLength - 1] replace /\w/ with "*" ++ (value[sizeOf(value) - visibleLength to -1])) 
    else 
      (value[0 to -1])) 
  else if (lower(visibleLocation) == "none") 
    value[0 to -1] replace /\w/ with "*" 
  else 
    value 

--- 
partialMasking(payload)
									

Example :

Input Payload:

										[{ 
"productId" : 1234567890, 
"name" : "mobile", 
"product_sku" : "onePlus9" 
}, 
{ 
"productId" : 12345, 
"name" : "laptop", 
"product_sku" : "HP14sLRTWJD" 
}] 
									

Expected output:

										[ 
  { 
    "productId": "12345*****", 
    "name": "mobil*", 
    "product_sku": "onePlus9" 
  }, 
  { 
    "productId": "12345", 
    "name": "lapto*", 
    "product_sku": "HP14sLRTWJD" 
  } 
] 
									

Script for above example:

										%dw 2.0 
output application/json  skipNullOn="everywhere" 

var fields = ["productId", "name"] 
var visibleLocation = "left" 
var visibleLength = 5 
fun partialMasking(data) = 
  data match { 
    case is Array -> if ($ == [] or $ == {}) 
      null 
    else 
      ($ map partialMasking($)) 
    case is Object -> if ($ == {}) 
      null 
    else 
      $ mapObject if ((fields contains ($$) as String) and !isEmpty($)) 
        { 
          ($$): masking($) 
        } 
      else 
        { 
          ($$): partialMasking($) 
        } 
    case is String -> data 
    else -> data 
  } 

fun masking(value: String) = 
  if (lower(visibleLocation) == "left") 
    (if (sizeOf(value) > visibleLength and visibleLength > 0) 
      (value[0 to visibleLength - 1] ++ (value[visibleLength to -1] replace /\w/ with "*")) 
    else 
      (value[0 to -1] )) 
  else if (lower(visibleLocation) == "right" and visibleLength > 0) 
    (if (sizeOf(value) > visibleLength) 
      (value[0 to sizeOf(value) - visibleLength - 1] replace /\w/ with "*" ++ (value[sizeOf(value) - visibleLength to -1])) 
    else 
      (value[0 to -1])) 
  else if (lower(visibleLocation) == "none") 
    value[0 to -1] replace /\w/ with "*" 
  else 
    value 

--- 
partialMasking(payload)
									

 

Conclusion:

This proposal is one of many functionalities. Soon I will provide you with another dose of information about what similar ways are available for xml payload. So far… .To make it more reusable, you can publish these functions as custom modules and directly use them by making function calls as we do for Mule provided modules.

 

 

References:

  1. https://docs.mulesoft.com/mule-runtime/4.3/dw-functions
  2. https://docs.mulesoft.com/mule-runtime/4.3/dataweave-create-module
Contact us