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
Please enter at least three characters to search
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Introducing the MERGE Command in PostgreSQL 15
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API

Trending

  • Role of Cloud Architecture in Conversational AI
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. How to Merge Payloads Using DataWeave

How to Merge Payloads Using DataWeave

DataWeave can do pretty much anything. You can use DataWeave to merge three different payloads into a single one using the service line value.

By 
Satheesh Kumar user avatar
Satheesh Kumar
·
Feb. 01, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
38.2K Views

Join the DZone community and get the full member experience.

Join For Free

There are a lot of questions on StackOverflow about merging multiple payloads into a single one using a primary key. However, the answers are a bit complex. We must know that DataWeave can do anything.

In this article, I am going to explain how to merge multiple payloads into a single one just using DataWeave.

Let's take this example where the profit, workforce, and investment of an organization are merged using service line number (Primary Key).

Main Flow
Image title

The payloads from different sources, as shown in the flow, are collected and stored in flow variables. The good thing about DataWeave is that it will allow you to map through flow variables.

Original payload (payload from Source 1):

{
"QuarterStatus":[
{
"Service line number":"4",
"Investment":220
},
{
"Service line number":"3",
"Investment":210
},  
{
"Service line number":"1",
"Investment":130
},
{
"Service line number":"2",
"Investment":452
}
]
}

Profit payload (payload from Source 2):

{
"Result":[
{
"Service line number":"4",
"Profit":23
},
{
"Service line number":"3",
"Profit":44
},
{
"Service line number":"2",
"Profit":76
},
{
"Service line number":"1",
"Profit":19
}
]
}

Workforce payload (payload from Source 3):

{
"Workforce":[
{
"Service line number":"4",
"Employees":200
},
{
"Service line number":"3",
"Employees":35
},
{
"Service line number":"1",
"Employees":342
},
{
"Service line number":"2",
"Employees":65
}
]
}

Let's store the payloads in variables

  • Payload 1: Original Payload Variable (note: before transformation, set this as the payload).

  • Payload 2: Store in flow as Variable 1.

  • Payload 3: Store in flow as Variable 2.

Now, use DataWeave to merge all three payloads into a single one using the service line value. Let's take the service line value as the primary key and filter the flow variables.

DW script:

%dw 1.0
%output application/json
---
{
OrganizationData: payload.QuarterStatus map ((payload1 , indexOfPayload1) -> {
"Service line number": payload1."Service line number",
Investment: payload1.Investment,
(flowVars.variable2.Workforce filter ($."Service line number" == payload1."Service line number")  map ((variable2 , indexOfVariable2) -> {
Employees: variable2.Employees
})),
(flowVars.variable1.Result filter ($."Service line number" == payload1."Service line number")  map ((variable1 , indexOfVariable1) -> {
Profit: variable1.Profit
}))
})
}

Output:

{
  "OrganizationData": [
    {
      "Service line number": "4",
      "Investment": 220,
      "Employees": 200,
      "Profit": 23
    },
    {
      "Service line number": "3",
      "Investment": 210,
      "Employees": 35,
      "Profit": 44
    },
    {
      "Service line number": "1",
      "Investment": 130,
      "Employees": 342,
      "Profit": 19
    },
    {
      "Service line number": "2",
      "Investment": 452,
      "Employees": 65,
      "Profit": 76
    }
  ]
}

Image title

That's all! The payload has merged successfully.

Payload (computing) Merge (version control)

Opinions expressed by DZone contributors are their own.

Related

  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Introducing the MERGE Command in PostgreSQL 15
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API

Partner Resources

×

Comments
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!