Updating fields with DataWeave made easy: The update operator

This new operator has been called the best thing to happen to DataWeave since sliced bread. It is the result of extensive work with our users on their day-to-day needs. In this blog post, you’ll see first hand how DataWeave scripts can be simplified in 4.3.0 by taking advantage of the latest feature to change field values.

While working on a Jenkins to Slack integration project, our goal was to obtain a list of failing tests from Jenkins builds and showing them in Slack, allowing users to assign them for review to themselves or others.

The Jenkins API requires XML while the Slack API, on the other hand, uses JSON extensively but also some URL encoded information. Internally, I decided to store the minimum information required for the current failing tests in JSON. As a result, DataWeave took center stage in the project — not only did I have to map between those two worlds but handle multiple formats.

Internal structure representing a failing test.

This was my first time using DataWeave so extensively and I was impressed with its power and simplicity. Handling the multiple formats was easy, in fact, I didn’t have to worry about them beyond defining which one I wanted, and I could quickly create the Slack messages to feature attachments and buttons. However, one thing struck me as odd about maintaining my internal structure through user interactions.

Changing values of a field

Since I wanted users in Slack to assign test reviewers, I had to maintain an assignment status: if there was no assignee, I would provide options to select one. Otherwise, I would show who the assignee was. So when a user-selected an assignee for a test, I had to update the assignee status. This was my initial approach:

Copying every single field seemed weird, so I searched for alternatives and landed on the “mapObject” function, to go through every key value pair, leaving all the same except the one I wanted to change. 

This was certainly better but it was still somewhat complicated and it was not something I could come up with on my own. That’s why I was excited about the introduction of a clear and simple way of updating fields in 4.3.0.

The update operator

This new operator allows you to update specific fields of a data structure with new values. Under the hood, DataWeave creates a brand new structure, keeping unaltered fields the same and updating the ones you selected. Here’s how my assignment script has been simplified in 4.3.0:

The update operator can also handle upserting the key-value pair. In the examples, the “assignee” field was kept with a null value to signal there was none but if instead, I would signal that by not having the key at all, my assignment script could use the upsert (!) functionality:

The great thing about this operator is that it makes it easy to update multiple fields and even doing so conditionally:

In the example above we used a variable “age” to capture the field value. This allows referencing it in conditionals and the new value definition.

Comparison of DataWeave field updates in 4.2 and 4.3


The update operator introduced in 4.3.0 is a true game-changer, allowing simple yet powerful actions to modify multiple fields on a payload. 

You can read all the details about the update operator here. We really hope you can take advantage of this new feature and start simplifying your scripts. Try it out today by signing up for a free trial of Anypoint Platform.