個別のオブジェクトの項目のマージ

DataWeave の例では、個別の入力配列の項目をマージします。最初の DataWeave 変数 (​firstInput​) は本 ID ごとに価格をリストし、2 番目の DataWeave 変数 (​secondInput​) は本 ID ごとに著者をリストします。各本には一意の ​bookId​ キーがあります。 開始する前に、DataWeave バージョン 2 (​%dw 2.0​) は Mule 4 アプリケーションを対象とすることに注意してください。Mule 3 アプリケーションの場合、Mule 3.9 ドキュメントセットの DataWeave バージョン 1 (​%dw 1.0​) の例を参照してください。他の Mule バージョンの場合は、目次の Mule Runtime バージョンセレクターを使用できます。

DataWeave スクリプトでは、同じ出力が生成されます。どちらのスクリプトも検索条件と共に 2 つの ​map​ 関数を使用しますが、一方のスクリプトは ​bookId​ の別名も作成します。

  • 最初の ​map​ 関数は、​firstInput​ 配列の要素を反復処理します。その関数が各要素を評価するときに、2 番目の ​map​ 関数は検索条件を使用して、​filter​ 条件 (​secondInput filter ($.*bookId contains firstInputValue.bookId)​) に一致する 2 番目の配列 (​secondInput​) の要素を識別します。この検索条件は、​firstInput​ 要素の ​bookId​ 値と一致する ​bookId​ 値が含まれる ​secondInput​ の要素を返します。

  • 2 番目の ​map​ 関数は、その絞り込まれた要素を評価し、​secondInputValue.author​ を使用して、オブジェクト ​{author : secondInputValue.author}​ の ​"author"​ 項目の値を選択して入力します。

  • filter​ は、2 番目の ​map​ 関数のスコープを同じ ​bookId​ を共有する ​secondInput​ のオブジェクトに制限します。

DataWeave スクリプト:
%dw 2.0
var firstInput = [
  { "bookId":"101",
    "title":"world history",
    "price":"19.99"
  },
  {
    "bookId":"202",
    "title":"the great outdoors",
    "price":"15.99"
  }
]
var secondInput = [
  {
    "bookId":"101",
    "author":"john doe"
  },
  {
    "bookId":"202",
    "author":"jane doe"
  }
]
output application/json
---
firstInput map (firstInputValue) ->
  {
    theId : firstInputValue.bookId as Number,
    theTitle: firstInputValue.title,
    thePrice: firstInputValue.price as Number,
    (secondInput filter ($.*bookId contains firstInputValue.bookId) map (secondInputValue) -> {
      theAuthor : secondInputValue.author
    })
  }
出力 JSON:
[
  {
    "theId": 101,
    "theTitle": "world history",
    "thePrice": 19.99,
    "theAuthor": "john doe"
  },
  {
    "theId": 202,
    "theTitle": "the great outdoors",
    "thePrice": 15.99,
    "theAuthor": "jane doe"
  }
]

次のスクリプトのように、​id​ の別名 (​using (id = firstInputValue.bookId)​ で作成される) を使用して同じスクリプトを記述することもできます。別名は、長いセレクター式 ​firstInputValue.bookId​ を置き換えます。

DataWeave スクリプト:
%dw 2.0
var firstInput = [
  { "bookId":"101",
    "title":"world history",
    "price":"19.99"
  },
  {
    "bookId":"202",
    "title":"the great outdoors",
    "price":"15.99"
  }
]
var secondInput = [
  {
    "bookId":"101",
    "author":"john doe"
  },
  {
    "bookId":"202",
    "author":"jane doe"
  }
]
output application/json
---
firstInput map (firstInputValue) -> using (id = firstInputValue.bookId)
  {
    theValue : id as Number,
    theTitle: firstInputValue.title,
    thePrice: firstInputValue.price as Number,
    (secondInput filter ($.*bookId contains id)  map (secondInputValue) -> {
      theAuthor : secondInputValue.author
    })
  }
出力 JSON:
[
  {
    "theValue": 101,
    "theTitle": "world history",
    "thePrice": 19.99,
    "theAuthor": "john doe"
  },
  {
    "theValue": 202,
    "theTitle": "the great outdoors",
    "thePrice": 15.99,
    "theAuthor": "jane doe"
  }
]

どちらのスクリプトも、同じ出力が生成されます。