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

Dataweave and the Reduce Operator: Part II

DZone's Guide to

Dataweave and the Reduce Operator: Part II

:array to :object transformations let us create maps from which a certain club's name can be easily looked up. However, they can't be done with the map operator.

· Integration Zone
Free Resource

Migrating from On-Prem to Cloud Middleware? Here’s what Aberdeen Group says leading companies should be considering. Brought to you in partnershp with Liaison Technologies

This post is a continuation of the series in which I am explaining the usage of the reduce operator of DataWeave.

In this post, I will be explaining how to perform :array to :object transformations. 

Let's observe the following input payload and output payload required:

Input payload: 

[{
"ClubID": "A1234",
"Name": "Barcelona FC",
"Country": "Spain"
}, {
"ClubID": "B1234",
"Name": "Chelsea FC",
"Country": "Spain"
}, {
"ClubID": "C1234",
"Name": "Arsenal FC",
"Country": "Englanda"
}]

Output payload: a map (An implementation of java.util.Map ) containing all the elements of the array with key as club ID and value as club name.

{
"A1234": "Barcelona FC",
"B1234": "Chelsea FC",
"C1234": "Arsenal FC"
}

This is basically an (:array to :object) transformation. This kind of transformation is very often required where we want to represent the data in terms of a unique ID from the payload, so that the data can be extracted conveniently using the ID. After creation of this map (when application/java mime type is used in output), a particular club's name can be easily looked up. For example, payload["A1234"] will give you  "Barcelona FC".

Transformation Details

Accumulator: {} (An empty object is chosen for addition of subsequent elements resulting from the lambda expression) 

Aggregate operation: + (concatinate: to create a map by concatinating all elements resulting from the lambda, the aggregation will basically be a concatination of multiple objects).

Dataweave code: 

payload reduce ((val, acc = {}) -> acc ++ (( { (val.ClubID) : ( val.Name ) } )))

If, however, the output required is : A map (e.g LinkedHashMap) of all the elements of the array with key as Club ID and value as rest of the club details. 

{
"A1234": {
"Name": "Barcelona FC",
"Country": "Spain"
},
"B1234": {
"Name": "Chelsea FC",
"Country": "Spain"
},
"C1234": {
"Name": "Arsenal FC",
"Country": "Englanda"
}
}

In this case, we just need to modify the lambda expression and keep the rest the same as the above transformation.

Dataweave code:

payload reduce ((val, acc = {}) -> acc ++ (( { (val.ClubID) : ( val - "ClubID" ) } )))

Advantage: Using the above transformation , we can easily look up all of the details of a particular club (when application/java mimetype is used in output). For example, to get all the Club details of the Club with ID "A1234," we can specify it like this: payload["A1234"]. This will result in :

{
"Name": "Barcelona FC",
"Country": "Spain"
}

This type of transformation is very often used in the merging of different payloads with a common Unique ID. I will be illustrating this in a later post.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
dataweave ,mulesoft ,reduce ,integration ,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 }}