← All articles
DataWeave · MuleSoft · Quick Reads

DataWeave map vs mapObject: When to Use Each

map and mapObject are two of the DataWeave functions you will use frequently, and the easiest way to choose between them is to look at the shape of the input and the shape you want to produce.

The short version is:

Array  → map       → Array
Object → mapObject → Object

That simple rule covers most day-to-day transformations.

Use map When You Are Transforming an Array

Suppose the payload contains Salesforce account records:

[
  {
    "Id": "001A",
    "Name": "Acme"
  },
  {
    "Id": "001B",
    "Name": "Global Media"
  }
]

You want a smaller array containing the fields expected by a downstream API.

%dw 2.0
output application/json
---
payload map (account) -> {
    accountId: account.Id,
    accountName: account.Name
}

Output:

[
  {
    "accountId": "001A",
    "accountName": "Acme"
  },
  {
    "accountId": "001B",
    "accountName": "Global Media"
  }
]

Each array item is transformed independently, and the result remains an array.

The $ Shortcut

For small transformations, DataWeave lets you use $ for the current item:

payload map {
    accountId: $.Id,
    accountName: $.Name
}

This is concise, but named parameters are often easier to read when the transformation becomes more complex:

payload map (account) -> {
    accountId: account.Id,
    accountName: account.Name
}

Readability usually matters more than saving a few characters.

Accessing the Array Index

map also provides the current index.

payload map (account, index) -> {
    sequence: index + 1,
    accountId: account.Id,
    accountName: account.Name
}

For two input records, the output contains sequence values 1 and 2.

The index is useful for formatting, diagnostics, or generating temporary sequence information. Avoid using an array position as a durable business identifier because ordering can change.

Use mapObject When You Are Transforming an Object

Now suppose the input is:

{
  "firstName": "Ravi",
  "lastName": "Shah",
  "country": "US"
}

If you want to transform the object's key/value pairs while still producing an object, use mapObject.

%dw 2.0
output application/json
---
payload mapObject (value, key) -> {
    (upper(key as String)): value
}

Output:

{
  "FIRSTNAME": "Ravi",
  "LASTNAME": "Shah",
  "COUNTRY": "US"
}

Here, mapObject iterates over the object's entries rather than over array elements.

Transforming Both Keys and Values

Because mapObject exposes the key and value, it is useful for generic object transformations.

Input:

{
  "firstName": "  Ravi  ",
  "lastName": "  Shah "
}

Transformation:

payload mapObject (value, key) -> {
    (key): trim(value)
}

Output:

{
  "firstName": "Ravi",
  "lastName": "Shah"
}

This type of transformation is useful when the same rule applies to every field rather than to a predefined set of field names.

A Common Mistake: Using map on an Object

Developers sometimes reach for map because they conceptually want to "map the fields."

But the question to ask is not what English verb describes the transformation. Ask what DataWeave data type you are iterating.

[ ... ] → array → map
{ ... } → object → mapObject

If the input is an array of objects, the outer transformation normally uses map:

payload map (record) -> {
    id: record.Id,
    name: record.Name
}

If you then need to dynamically transform fields inside each record, mapObject can be nested inside that map.

Using map and mapObject Together

Consider an array where each record contains a dynamic attributes object:

[
  {
    "id": "A1",
    "attributes": {
      "region": "us",
      "tier": "gold"
    }
  },
  {
    "id": "A2",
    "attributes": {
      "region": "uk",
      "tier": "silver"
    }
  }
]

You can transform the array with map and the nested object with mapObject:

%dw 2.0
output application/json
---
payload map (record) -> {
    id: record.id,
    attributes: record.attributes mapObject (value, key) -> {
        (upper(key as String)): upper(value)
    }
}

The two functions are not alternatives in every situation. They frequently work together because enterprise payloads contain arrays nested inside objects and objects nested inside arrays.

Quick Decision Table

InputDesired outputUsually use
ArrayArraymap
ObjectObjectmapObject
Array of Salesforce recordsTransformed record arraymap
Dynamic object fieldsTransformed objectmapObject
Array containing dynamic objectsArray with transformed nested objectsmap + mapObject

Practical Rule

When you are unsure, look at the value immediately before the function:

someArray map ...

or:

someObject mapObject ...

Then ask what shape the result should have.

That habit makes map versus mapObject much easier to reason about than memorizing syntax.