Dark theme | Light theme

February 15, 2022

DataWeave Delight: Trimming string values

In DataWeave we can use the trim function from the dw::Core module to remove any space character before and after a string value. The characters that are removed are the space, tab and newline characters. There is an overloaded function definition that will accept a null value and returns a null value.

In the following example we trim string values that start and end with spaces, tabs and newline characters:


%dw 2.0

output application/json
    // Spaces at the start and end ar removed.
    spaces: trim("   mrhaki  "),

    // Also tabs are removed.
    tabs: trim("\t mrhaki"),

    // And newline characters are removed
    newline: trim("\tmrhaki \r\n"),

    // trim will return null when a null value is used.
    nullValue: trim(null),


  "spaces": "mrhaki",
  "tabs": "mrhaki",
  "newline": "mrhaki",
  "nullValue": null

Written with DataWeave 2.4.