Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DataWeave and the Reduce Operator: Part I

DZone's Guide to

DataWeave and the Reduce Operator: Part I

Reduce is a powerful operator that can be used on an array. Specifically, it can be used to process an :array and operate on each of its elements.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Reduce is a powerful operator that can be used on an array (DataWeave :array datatype).

Reduce can be used to process an :array and operate on each of its elements. It performs an aggregation operation on the elements of the :array after performing a lambda operation (optional) on each of its element.

It can be used as a substitute to the map operator. The :array to :object is also possible via the reduce operator. We use the reduce operator to do this kind of transformation, in addition to various other types.

In this series, I will provide examples of how to use the reduce operator effectively.

For the reduce operations, an accumulator should be chosen according to the aggregate result we want to achieve. Also, the aggregation operator — for example, + (Add), ++ (Concat), * (Multiply), etc. — should be chosen as per requirements.

Execute a Simple Arithmetic Sum of a List's Elements 

Let's say the list elements are [10, 20, 30, 40].

  • Expected output: 100 (sum of all elements of the array).

  • Accumulator: 0 (a number zero is chosen for the addition of subsequent elements).

  • Aggregate operation: + (addition).

  • Dataweave code: payload reduce $$ + $.

We can execute a lambda over each element of the array to get a modified element and add it continuously to the accumulator.

Perform a More Complex Arithmetic Operation

Input payload (a list):

[{number : 10, times : 8},{number : 2, times : 5},{number : 7, times : 7},{number : 6, times : 3},{number : 4, times : 7}]

Expected output:  185 (sum of each element resulting from "number" multiplied by "times").

Accumulator: 0 (a number zero is chosen for the addition of subsequent elements).

Aggregate operation: + (addition).

Dataweave code:

payload reduce ((val, acc = 0) -> acc + (val.number * val.times))  

Modify a List

Input payload: 

[{"Capital": "New Delhi","Country": "India"}, {"Capital": "Paris","Country": "France"}, {"Capital": "London","Country": "England"}]

Required output:

["India-New Delhi","France-Paris","England-London"] 

Accumulator: [] (an empty array is chosen for addition of subsequent elements resulting from the lambda).

Aggregate operation: + (addition operation to obtain an array of the elements resulting from lambda; this is used to make an array push on the accumulating array using the modified element).

Dataweave code:


payload reduce ((val, acc = []) -> acc + ( (val.Country) ++ "-" ++ val.Capital ))

Note: This can also be done using the map operator.

I hope that this post helps you get started using the reduce operator,

In the next post, I will try to explain and exemplify some scenarios (:array to :object)  using the reduce operator.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mulesoft ,dataweave ,integration ,reduce ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}