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

Mule 4(DataWeave 2.0)OuterJoin结果处理:扁平化及新增静态属性

Solution for Flattening & Transforming Outer Join Results in DataWeave 2.0

Got it, let's get your DataWeave output into the exact format you need. The outerJoin gives us structured l (left array) and r (right array) objects, so we just need to flatten those, rename keys, add your static property, and tweak the structure.

First, let's address the typo in your expected output: the second object has "Key2": "2000" which I assume is a mistake (should be "Key1": "2000"). I'll provide two versions below—one that fixes that logical typo, and another if you actually need dynamic Key1, Key2, Key3 labels for the ID values.

Version 1: Logical Key Naming (Key1 for ID, Key2 for ID_2/EID)

This matches what I think you intended, with consistent key names across all objects:

%dw 2.0
import * from dw::core::Arrays
output application/json
var var1 = [
 {"ID": 1000, "ID_2": "ABC-1000"},
 {"ID": 2000, "ID_2": "DEF-2000"},
 {"ID": 3000, "ID_2": "GHI-3000"}
]
var var2 = [
 {"EID": "ABC-1000", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
 {"EID": "DEF-2000", "GUID": "63F6A192988A4C0696B117E9AC080611"},
 {"EID": "GHI-3000", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]
---
outerJoin(var1, var2, (obj) -> obj.ID_2, (obj) -> obj.EID) map (joinResult) -> {
    Key1: joinResult.l.ID as String, // Convert ID to string as shown in expected output
    Key2: joinResult.l.ID_2,
    Name: "Test", // Static property
    GUID: joinResult.r.GUID
}

Output:

[
{"Key1": "1000", "Key2": "ABC-1000", "Name": "Test", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
{"Key1": "2000", "Key2": "DEF-2000", "Name": "Test", "GUID": "63F6A192988A4C0696B117E9AC080611"},
{"Key1": "3000", "Key2": "GHI-3000", "Name": "Test", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]

Version 2: Dynamic Key Names (Key1, Key2, Key3 for IDs)

If you actually need incrementing key names for the ID values (as shown in your expected output), use mapIndexed to generate dynamic keys:

%dw 2.0
import * from dw::core::Arrays
output application/json
var var1 = [
 {"ID": 1000, "ID_2": "ABC-1000"},
 {"ID": 2000, "ID_2": "DEF-2000"},
 {"ID": 3000, "ID_2": "GHI-3000"}
]
var var2 = [
 {"EID": "ABC-1000", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
 {"EID": "DEF-2000", "GUID": "63F6A192988A4C0696B117E9AC080611"},
 {"EID": "GHI-3000", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]
---
outerJoin(var1, var2, (obj) -> obj.ID_2, (obj) -> obj.EID) mapIndexed ((joinResult, index) -> {
    ("Key$(index + 1)"): joinResult.l.ID as String, // Dynamic key name
    Key2: joinResult.l.ID_2,
    Name: "Test",
    GUID: joinResult.r.GUID
})

Output:

[
{"Key1": "1000", "Key2": "ABC-1000", "Name": "Test", "GUID": "A0F4960AE5E54459A1B1FB40297EF893"},
{"Key2": "2000", "Key2": "DEF-2000", "Name": "Test", "GUID": "63F6A192988A4C0696B117E9AC080611"},
{"Key3": "3000", "Key2": "GHI-3000", "Name": "Test", "GUID": "A0FB600FA73E4E3BBF1CB59AA6E92AEF"}
]

Key Changes Explained:

  • map/mapIndexed: Iterates over each join result to transform the structure.
  • Key Renaming: Explicitly maps ID to your desired key name, and ID_2 to Key2.
  • Static Property: Adds the hardcoded "Name": "Test" directly in the object.
  • Type Conversion: Converts ID from a number to a string to match your expected output.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:32:52