Skip to main content
Contact Us 1-800-596-4880

Concatenation functions tips and tricks in DataWeave

5 min read

In DataWeave 2.0, concatenation can be achieved by using the ++ (plus plus) function. However, there are two additional syntax options to concatenate objects and one to concatenate strings in DataWeave. Concatenation is when you link two strings, objects, data types etc together in a chain or series.

In many cases, when dealing with large datasets, these different concatenation methods may be helpful to you to make data transformations easier. In this tutorial, we are going to highlight a few functions. You can test these functions out yourself in the DataWeave Playground, in Visual Studio Code, or in Anypoint Studio using a Transform Message component. The screenshots from this tutorial were created using the DataWeave Playground.

Using ++ to concatenate objects and strings

You can achieve string concatenation from two or more strings, using the ++ function. Here’s an example:

image

Open in Playground

You can also concatenate two or more objects and get one flattened object in return, using the ++ function, like this:

image

Open in Playground

Using $() to concatenate strings

We already reviewed how to concatenate two or more strings when using the ++ function, but sometimes you need to keep adding spaces in between strings, and you end up with something like this:

image

Open in Playground

If you surround your variables in $(), you can keep writing in a single line as if it was just one string, instead of using the ++ function for each variable. This removes the extra space and I find it cleaner to concatenate several strings.

image

Open in Playground

Notice that I used the ++ function for the last concatenation because str5 and str6 didn’t need a space in-between.

What $() is doing is executing the expression that’s inside and returns a string that can be concatenated with the rest. You can’t create expressions that will not be able to be converted into a string. For example, if you try to concatenate an array with this syntax, you will get an error stating that it was expecting a string.

image

Open in Playground

However, if you work with numbers, DataWeave can transform the result into a string without a problem. You may get a warning saying something like “Auto-Coercing type from: Number to: String.” You can avoid this by explicitly transforming the expression into a string. However, this is not necessary to correctly run the application.

image

Open in Playground

Using the object destructor to concatenate objects

Another way to concatenate objects involves using the object destructor. This is done by surrounding an object in parentheses (), and then surrounding those in curly brackets {}. The object destructor is capable of destroying an array containing objects or destroying an object into key/value pairs. The outer curly brackets {} will then construct a new object containing these key/value pairs.

An example of this syntax, using the previous example, would go like this:

image

Open in Playground

You’ll notice that you have to surround each object in parentheses for it to work. As mentioned, the object destructor can also destroy an array containing objects. Below is an example of how to do this:

image

Open in Playground

When using this function, you don’t need to surround each object by parentheses because you’re surrounding the array with them. Thus, the array contains the objects to be concatenated. I hope that these examples were useful to you and you feel confident in concatenating functions in DataWeave using more than just the ++ function.

Next steps

Hope you enjoyed this tutorial on DataWeave concatenation. If you found this article helpful, please rate the tutorial below. This article only covered the very basics of what’s possible with DataWeave, make sure to checkout the more tutorials on DataWeave by viewing our full catalog of developer tutorials.

Try Anypoint Platform for free

Start your 30-day free trial of the #1 platform for integration, APIs, and automation. No credit card required. No software to install.

Try for free

anypoint product trial zigzag