如何使用DataWeave将第二个Payload的账户对象追加到第一个Payload的账户数组且不影响其余数据
DataWeave: Append Accounts from Second Payload to First Payload's Accounts Array
Here's a straightforward DataWeave 2.0 solution to append the accounts from your second payload to the first payload's accounts array, while preserving all other existing data in the first payload:
%dw 2.0 output application/json var payload1 = [ { "_id" : "7c2a355a-1fca-11ed-861d-0242ac120002", "accounts" : [ { "account_id" : "b206ae7e-1fca-11ed-861d-0242ac120002", "account number" : 123456, "current balance" : { "$number decimal" : "0" }, "daily interest rate" : null, "collateral" : [ { "loan collateral id" : "03501efa-1fcb-11ed-861d-0242ac120002", "model" : "car" } ] }, { "account_id" : "416535ee-1fcc-11ed-861d-0242ac120002", "account number" : 594154, "current balance" : { "$number decimal" : "0" }, "daily interest rate" : null, "collateral" : [ { "loan collateral id" : "416535ee-1fcc-11ed-861d-0242ac120002", "model" : "car" } ] }, { "account_id" : "416535ee-1fcc-11ed-861d-0242ac120002", "account number" : 258963, "current balance" : { "$number decimal" : "0" }, "daily interest rate" : null, "collateral" : [ { "loan collateral id" : "65736c26-1fcc-11ed-861d-0242ac120002", "model" : "car" } ] } ], "related entities" : [ { "Entity_id" : "8a107a20-1fcb-11ed-861d-0242ac120002", "shortname" : "name", "addresses" : [ { "Entity_address_id" : "ca66ac66-1fcb-11ed-861d-0242ac120002", "city" : "XXX", "state" : "xxx" } ] } ] } ] var payload2 = [ { "account_id" : "067094ce-1fcc-11ed-861d-0242ac120002", "account number" : 464634, "current balance" : { "$number decimal" : "0" }, "daily interest rate" : null, "collateral" : [ { "loan collateral id" : "1c971994-1fcc-11ed-861d-0242ac120002", "model" : "car" } ] }, { "account_id" : "24835d3e-1fcc-11ed-861d-0242ac120002", "account number" : 14654163, "current balance" : { "$number decimal" : "0" }, "daily interest rate" : null, "collateral" : [ { "loan collateral id" : "03501efa-1fcb-11ed-861d-0242ac120002", "model" : "car" } ] } ] --- payload1 map (customer) -> { customer, accounts: customer.accounts ++ payload2 }
How this works:
- We start by defining
payload1andpayload2as variables to replicate your input data (in a real Mule application, you'd replace these with your actual input sources, like the mainpayloadfor the first dataset and a variable/secondary input for the second). - The
mapfunction iterates over each object in the first payload's array (since your first payload is a single-element array, this processes the customer record). - We first include all existing fields from the customer record with
customer, then explicitly update theaccountsfield by merging the originalaccountsarray with the entirepayload2array using the++array concatenation operator. - This ensures all other fields (like
_idandrelated entities) remain untouched, while the new accounts are appended to the end of theaccountslist.
The output will exactly match your expected result, with a total of 5 accounts in the accounts array.
内容的提问来源于stack exchange,提问作者Buddy
相关产品推荐
相关产品推荐

