BookatlasTwo-Day MuleSoft → Salesforce Bootcamp
50 / 1048 · Module 3 — DataWeave 2.0 Crash Course← prev⊞ allnext →

3.7Arrays: the really important part

Suppose payload is:

[
  {
    "id": "1",
    "name": "Acme"
  },
  {
    "id": "2",
    "name": "Widget Corp"
  },
  {
    "id": "3",
    "name": "MegaCorp"
  }
]

You very often need to transform every record.

That's:

map

Example:

%dw 2.0
output application/json
---
payload map (customer) -> {
    customerId: customer.id,
    companyName: customer.name
}

Output:

[
  {
    "customerId": "1",
    "companyName": "Acme"
  },
  {
    "customerId": "2",
    "companyName": "Widget Corp"
  },
  {
    "customerId": "3",
    "companyName": "MegaCorp"
  }
]

Think TypeScript:

payload.map(customer => ({
    customerId: customer.id,
    companyName: customer.name
}));

Almost identical conceptually.