---
schema_version: "1"
id: 36eda6d4d0285c9ef5736d5bf72fb7afc445e15654e6997e8d8bfc99b67510fc
title: Dynamically Map Based on a Definition
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook-map-based-on-an-external-definition.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-based-on-an-external-definition
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2ab5147e201a00a39c019c90d5b2d591bdcae8d8e9b8a64e0da5d773f47cebb2
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Dynamically Map Based on a Definition

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

You can create a transformation that can dynamically change what it does depending on a definition
input. This DataWeave example receives both a payload input, and a variable named `mappings` that
specifies how to rename each field and what default value to use in each.

The example uses the following:

- A `map` function to go through all of the elements in the input array. Also a second `map`
  function to go through each field in each element.

- A custom function that applies the changes specified in the `mappings` variable.

- `default` to set a default value, that comes from the `mappings` variable.

DataWeave Script:

%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) )

Input XML Payload:

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

Input JSON Mule Event Variable (`vars.mappings`):

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

Output JSON:

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

## Related Examples

- [Rename Keys](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-rename-keys)

- [Exclude Fields from the Output](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-exclude-field)

- [Output a Field When Present](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-output-a-field-when-present)

## See Also

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.3/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook)
