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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Introducing the MERGE Command in PostgreSQL 15

Trending

  • Your Cloud Infrastructure Is Elastic, and So Should Be Your Cost Governance
  • Hosted Code Interpreter in Spring AI: Letting OpenAI Write and Run Python via the Responses API
  • Age Verification Is Becoming One of Tech’s Hardest QA Problems
  • The 20 Software Engineering Laws

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 01, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
38.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Introducing the MERGE Command in PostgreSQL 15

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook