Concatenation functions tips and tricks in DataWeave

tips and tricks
MuleSoft Ambassador Team
MuleSoft Ambassador Team
5 min read

We would like to thank MuleSoft Ambassador, Alexandra Martinez for her contribution to this developer tutorial.


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 Anypoint Studio by opening up a transform message component and sending sample data to the DataWeave function. 

Using the ++ function to concatenate objects and strings

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

hello mule

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


Here is the example code used in the script above if you’d like to try yourself:

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:


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 4

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 5

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 6
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 9

Here is the example code used in the script above if you want to try this concatenation function.

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 8

Here is the example code used in the script above if you want to follow along:

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.


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 by clicking here.

Try Anypoint Platform for free

Start free trial

Already have an account? Sign in.

Related tutorials