Search

Dark theme | Light theme

July 12, 2022

DataWeave Delight: Define Multi Line Strings

To define a string value in DataWeave we can use both double quotes or single quotes. To define a multi line string we simply define a string value over multiple lines. We don't have to do any strange concatenation, but simply define the value over multiple lines. DataWeave will add an end-of-line character automatically after each line.

In the following example we use different ways to defined single and multi line strings:

Source

%dw 2.0

import lines from dw::core::Strings

output application/json
---
{
    doubleQuotes: "String value defined using double quotes",
    singleQuotes: 'String value defined using single quotes',

    // Multi line strings can be defined as well.
    multiLineDoubleQuotes: "This is a multi line
string value
with double quotes",
    multiLineSingleQuotes: 'This is a multi line
string value with 
single quotes',

    // We can use the lines function to transform
    // each line into an element in an array.
    multiLines: lines("Multiline
string transformed
to array of strings")
}

Output

{
  "doubleQuotes": "String value defined using double quotes",
  "singleQuotes": "String value defined using single quotes",
  "multiLineDoubleQuotes": "This is a multi line\nstring value\nwith double quotes",
  "multiLineSingleQuotes": "This is a multi line\nstring value with \nsingle quotes",
  "multiLines": [
    "Multiline",
    "string transformed",
    "to array of strings"
  ]
}

Written with DataWeave 2.4.