DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • Spring WebFlux: publishOn vs subscribeOn for Improving Microservices Performance

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Streamlining Event Data in Event-Driven Ansible
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)

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.

By 
Rahul Kumar user avatar
Rahul Kumar
·
Feb. 27, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.3K Views

Join the DZone community and get the full member experience.

Join For Free

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

Array to String - Obtaining an Aggregated String from an Array

Input Payload:

["A","B","C","D","E","F","G","H"]

Expected output: (Dasherized string)

"ABCDEFGH"

Accumulator: "" (Empty String into which elements will be concatenated).

Aggregate operation: ++ (Concat)

Dataweave Code: 

payload reduce ((character,acc="") -> acc ++ character )

Here character references each element of the array i.e. A, B, C and so on.

The expression acc ++ character adds each character subsequently into the accumulator which becomes the output at the end.

Array to Object - Obtaining an Aggregated Object/Map from an Array

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"
}]

1. Output payload: a map/Object 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 the addition of subsequent elements resulting from the lambda expression) 

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

Dataweave code: 

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


2. If, however, the output required is : A map/Object of all the elements of the array with the key as Club ID and value as the rest of the club details. 

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

In this case, we just need to modify the 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. 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.

Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • Spring WebFlux: publishOn vs subscribeOn for Improving Microservices Performance

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: