---
schema_version: "1"
id: 740b05ef1f452d5a078f9e4222f45361491c83ecfcc4f919f38cc0da378e1bb5
title: 定義に基づいた動的なマップ
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.5/dataweave-cookbook-map-based-on-an-external-definition.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.5/dataweave-cookbook-map-based-on-an-external-definition
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f7f3f7ff950c2cd43af5b499efdf121272ff9e5577c7d7a673705f510811b779
description: MuleSoft Documentation Site
release: "2.5"
latest: false
---
# 定義に基づいた動的なマップ

定義入力に応じて動的に実行内容を変更できる変換を作成できます。この DataWeave の例では、ペイロード入力と ​`mappings`​ という名前の変数の両方を取得します。この変数は、各項目の名前の変更方法とそれぞれで使用するデフォルト値を指定します。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

この例では、以下を使用します。

- 入力配列のすべての要素を調べる ​`map`​ 関数。および、各要素の各項目を調べる 2 番目の ​`map`​ 関数。
- `mappings`​ 変数で指定された変更を適用するカスタム関数。
- デフォルト値を設定する ​`default`​。これは、​`mappings`​ 変数から取得します。

DataWeave スクリプト:

%dw 2.0
output application/json
var applyMapping = (in, mappingsDef) -> (
   mappingsDef map (def) -> {
    (def.target) : in\[def.source\] default def."default"
  }
)
---
payload.sfdc\_users.\*sfdc\_user map (user) -> (
        applyMapping(user, vars.mappings)
)

入力 XML ペイロード:

```xml
<sfdc_users>
    <sfdc_user>
      <sfdc_name>Mariano</sfdc_name>
      <sfdc_last_name>Achaval</sfdc_last_name>
      <sfdc_employee>true</sfdc_employee>
    </sfdc_user>
    <sfdc_user>
      <sfdc_name>Julian</sfdc_name>
      <sfdc_last_name>Esevich</sfdc_last_name>
      <sfdc_employee>true</sfdc_employee>
    </sfdc_user>
    <sfdc_user>
      <sfdc_name>Leandro</sfdc_name>
      <sfdc_last_name>Shokida</sfdc_last_name>
    </sfdc_user>
</sfdc_users>
```

入力 JSON Mule イベント変数 (​`vars.mappings`​):

```json
[
  {
    "source": "sfdc_name",
    "target": "name",
    "default": "---"
  },
  {
    "source": "sfdc_last_name",
    "target": "lastName",
    "default": "---"
  },
  {
    "source": "sfdc_employee",
    "target": "user",
    "default": true
  }
]
```

出力 JSON:

```json
[
  [
    {"name": "Mariano"},
    {"lastName": "Achaval"},
    {"user": "true"}
  ],
  [
    {"name": "Julian"},
    {"lastName": "Esevich"},
    {"user": "true"}
  ],
  [
    {"name": "Leandro"},
    {"lastName": "Shokida"},
    {"user": true}
  ]
]
```

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

- [キーの名前の変更](https://docs.mulesoft.com/jp/dataweave/2.5/dataweave-cookbook-rename-keys)
- [出力からの項目の除外](https://docs.mulesoft.com/jp/dataweave/2.5/dataweave-cookbook-exclude-field)
- [存在する項目の出力](https://docs.mulesoft.com/jp/dataweave/2.5/dataweave-cookbook-output-a-field-when-present)

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

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