---
schema_version: "1"
id: 079032cbea07015bd7d32c3ddb3f49ac4bf3d64c475b71871a478eb60c06847b
title: 個別のオブジェクトの項目のマージ
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.8/dataweave-cookbook-merge-multiple-payloads.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook-merge-multiple-payloads
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 824a06e6166360d9ffdd88a0fbf9614eff68a3ad4150eb02967b78b002eaf092
description: MuleSoft Documentation Site
release: "2.8"
latest: false
---
# 個別のオブジェクトの項目のマージ

DataWeave の例では、個別の入力配列の項目をマージします。最初の DataWeave 変数 (​`firstInput`​) は本 ID ごとに価格をリストし、2 番目の DataWeave 変数 (​`secondInput`​) は本 ID ごとに著者をリストします。各本には一意の ​`bookId`​ キーがあります。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

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 スクリプト:

```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:

```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 スクリプト:

```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:

```json
[
  {
    "theValue": 101,
    "theTitle": "world history",
    "thePrice": 19.99,
    "theAuthor": "john doe"
  },
  {
    "theValue": 202,
    "theTitle": "the great outdoors",
    "thePrice": 15.99,
    "theAuthor": "jane doe"
  }
]
```

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

## [](#関連する例)関連する例

- [データの抽出](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook-extract-data)
- [複数の入力の参照](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook-reference-multiple-inputs)
- [複数の配列要素のグループ化](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook-zip-arrays-together)

## [](#関連情報)関連情報

- [DataWeave セレクター](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-selectors)
- [DataWeave の解説書](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook)