You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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 payload1 and payload2 as variables to replicate your input data (in a real Mule application, you'd replace these with your actual input sources, like the main payload for the first dataset and a variable/secondary input for the second).
  • The map function 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 the accounts field by merging the original accounts array with the entire payload2 array using the ++ array concatenation operator.
  • This ensures all other fields (like _id and related entities) remain untouched, while the new accounts are appended to the end of the accounts list.

The output will exactly match your expected result, with a total of 5 accounts in the accounts array.

内容的提问来源于stack exchange,提问作者Buddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:37:26