If you are working with data transformations in MuleSoft, handling massive blocks of text is practically a daily ritual. One of the most powerful (yet often overlooked) tools in your toolkit is the lines function found in DataWeave (DWL).
This guide will walk you through exactly how to use lines to tame strings riddled with line breaks. Think of the lines function as a pair of digital scissors: it takes a long string and snips it into neat, individual strips (an array) every time it encounters a newline (\n) or a carriage return (\r).
Example 1: Splitting a String by Line Breaks
Let's start with the basics. Imagine you have a raw string containing text separated by line breaks.
The Scenario
We have a variable called myString. To the human eye, it looks like a paragraph, but for our data processing needs, we need it structured as a list (array).
The Code
Here is how we set up the transformation. We import the Strings module and apply the function:
%dw 2.0
import * from dw::core::Strings
output application/json
var myString = "Hello,\nthis is a\nsample string."
---
/*
If we want to split this string by line breaks and
store each line in an array, we can use the lines function like this:
*/
lines(myString)
The Output
The function automatically detects the breaks and returns an array of three distinct strings:
[
"Hello,",
"this is a",
"sample string."
]
Once your data is in an array, you have full control! You can access specific lines using index notation, such as myArray[0] to get the first line or myArray[2] for the last.
Example 2: Reversing the Process with joinBy
Sometimes you need to do the exact opposite—stitch an array of strings back together into a single block of text. This is where the joinBy function steps in.
Unlike lines, the joinBy function takes an array and a "separator" (like a comma, a space, or a new line) and merges them into one string.
The Code
var myString = joinBy(myArray, "\n")
The Output
This returns us to our initial state, restoring the line breaks:
"Hello,\nthis is a\nsample string."
Example 3: Advanced Handling (Parsing CSV Data)
This is where the lines function truly shines. A very typical use case in MuleSoft integration is receiving raw data that looks like a CSV file—a simple string with headers and values—and needing to convert that into a structured JSON object.
The Input String
Imagine your input (payload) looks like this:
var myString = "name,age,gender\nAlice,25,F\nBob,30,M\nCharlie,28,M"
To transform this efficiently, we need to perform the following logic:
- Split the string using
lines. - Isolate the header row (Index 0).
- Map through the rest of the rows (Index 1 to the end).
- Split the individual columns by commas.
The Transformation Code
Here is the script to turn that raw string into an array of objects with name, age, and gender attributes. Note that payload below refers to the CSV string:
%dw 2.0
output application/json
import * from dw::core::Strings
var myArray = lines(payload)
---
myArray[1 to -1] map(
{((myArray[0] splitBy ",") map ((item, index) ->
(item) : ($ splitBy ",")[index]
))}
)
How It Works
We use a lambda expression inside the map function. The code dynamically grabs the headers from myArray[0] and matches them to the data values in the current row ($).
The Final Output
The result is a clean, usable JSON array:
[
{
"name": "Alice",
"age": 25,
"gender": "F"
},
{
"name": "Bob",
"age": 30,
"gender": "M"
},
{
"name": "Charlie",
"age": 28,
"gender": "M"
}
]
You can now access specific data points easily. For example, myArray[1] gives you Bob's object, or you can reference specific properties like name.
Conclusion
Working with strings that contain line breaks doesn't have to be a headache. By leveraging DWL's lines function, you can parse strings line-by-line, while joinBy helps you put them back together. Whether you are doing simple text formatting or complex CSV parsing, these tools are essential for any MuleSoft developer.
I hope this post gave you fresh insight into DataWeave!