Combining Objects: Concatenation in DW 2.0

Updated: Sep 4

The video version of this article can be found at the end of the post.

In this post, I will explain what object concatenation is and how to utilize it.

You may be familiar with other language’s concatenation functions, especially when using two strings. For example:

“Hello”.concat(“ World!);
“Hello” + “ World!;
print(“Hello”, “ World!)
Concatenate ‘string “Hello” “ World!

This can also be achieved in DataWeave with the ++ function.

“Hello” ++ “ World”

But, can this also be used for objects? Keep reading to find out!

What is object concatenation?

When I say “object concatenation”, I mean the action of combining 2 different objects, and creating one single object containing all the fields (or keys) and values of these objects.

For example, we have these 2 JSON objects:

key1 value1 key2 key3 value3 key4 value4 key5 key6 value6

And we want to achieve this:

key1 value1 key2 key3 value3 key4 value4 key5 key6 value6

In the end, we have a single object, that contains everything that the other 2 objects had. This is object concatenation.

Object concatenation using ++

The most commonly used way to achieve object concatenation in DataWeave, is the ++ function.

The previous operation, using DW, would look something like this:

dw 2.0 output application json var object1 key1 value1 key2 key3 value3 var object2 key4 value4 key5 key6 value6 object1 plus plus ++ object2