DataWeave Mapping Examples Explained | MuleSoft Guide

Example 1: Basic Mapping

Input:

[  {"name": "John", "age": 30},  {"name": "Jane", "age": 25}]

Output:

[  "John",  "Jane"]

Script:

%dw 2.0output application/jsonvar input = [  {"name": "John", "age": 30},  {"name": "Jane", "age": 25}]---input map ((item) -> item.name)

Example 2: Mapping with Calculated Value

Input:

[  {"product": "A", "price": 100},  {"product": "B", "price": 150}]

Output:

[  {"product": "A", "finalPrice": 120},  {"product": "B", "finalPrice": 180}]

Script:

%dw 2.0output application/jsonvar input = [  {"product": "A", "price": 100},  {"product": "B", "price": 150}]---input map ((item) -> {  product: item.product,  finalPrice: item.price * 1.2})

Example 3: Filtering with Mapping

Input:

[  {"name": "Alice", "age": 22},  {"name": "Bob", "age": 17}]

Output:

[  {"name": "Alice"}]

Script:

%dw 2.0output application/jsonvar input = [  {"name": "Alice", "age": 22},  {"name": "Bob", "age": 17}]---input filter (item) -> item.age >= 18 map ((item) -> {  name: item.name})

Example 4: Nested Mapping

Input:

{  "employees": [    {"name": "Jake", "skills": ["Java", "Python"]},    {"name": "Anna", "skills": ["JavaScript", "C#"]}  ]}

Output:

[  ["Java", "Python"],  ["JavaScript", "C#"]]

Script:

%dw 2.0output application/jsonvar input = {  "employees": [    {"name": "Jake", "skills": ["Java", "Python"]},    {"name": "Anna", "skills": ["JavaScript", "C#"]}  ]}---input.employees map ((employee) -> employee.skills)

Example 5: Mapping to a New Object Structure

Input:

[  {"id": 1, "title": "Task 1"},  {"id": 2, "title": "Task 2"}]

Output:

[  {"taskId": 1, "taskTitle": "Task 1"},  {"taskId": 2, "taskTitle": "Task 2"}]

Script:

%dw 2.0output application/jsonvar input = [  {"id": 1, "title": "Task 1"},  {"id": 2, "title": "Task 2"}]---input map ((item) -> {  taskId: item.id,  taskTitle: item.title})

Example 6: Mapping with Conditional Logic

Input:

[  {"name": "Paul", "score": 85},  {"name": "Sara", "score": 40}]

Output:

[  "Pass",  "Fail"]

Script:

%dw 2.0output application/jsonvar input = [  {"name": "Paul", "score": 85},  {"name": "Sara", "score": 40}]---input map ((item) -> if (item.score >= 50) "Pass" else "Fail")

Example 7: Mapping with String Manipulation

Input:

[  {"name": "Charlie"},  {"name": "Diana"}]

Output:

[  "Charlie - processed",  "Diana - processed"]

Script:

%dw 2.0output application/jsonvar input = [  {"name": "Charlie"},  {"name": "Diana"}]---input map ((item) -> item.name ++ " - processed")

Example 8: Creating a List of Objects

Input:

[  {"item": "Book", "quantity": 2},  {"item": "Pen", "quantity": 5}]

Output:

[  {"item": "Book", "total": 200},  {"item": "Pen", "total": 500}]

Script:

%dw 2.0output application/jsonvar input = [  {"item": "Book", "quantity": 2},  {"item": "Pen", "quantity": 5}]---input map ((item) -> {  item: item.item,  total: item.quantity * 100})

Example 9: Mapping to a Specific Format

Input:

[  {"date": "2023-01-01"},  {"date": "2023-02-01"}]

Output:

[  "01/01/2023",  "01/02/2023"]

Script:

%dw 2.0output application/jsonvar input = [  {"date": "2023-01-01"},  {"date": "2023-02-01"}]---input map ((item) -> item as Date{format:"yyyy-MM-dd"} as String{format:"dd/MM/yyyy"})

Example 10: Selecting Specific Fields

Input:

[  {"id": 1, "name": "Eve", "email": "eve@example.com"},  {"id": 2, "name": "Tom", "email": "tom@example.com"}]

Output:

[  {"name": "Eve"},  {"name": "Tom"}]

Script:

%dw 2.0output application/jsonvar input = [  {"id": 1, "name": "Eve", "email": "eve@example.com"},  {"id": 2, "name": "Tom", "email": "tom@example.com"}]---input map ((item) -> {  name: item.name})

Example 11: Combining Fields

Input:

[  {"firstName": "Rick", "lastName": "Sanchez"},  {"firstName": "Morty", "lastName": "Smith"}]

Output:

[  "Rick Sanchez",  "Morty Smith"]

Script:

%dw 2.0output application/jsonvar input = [  {"firstName": "Rick", "lastName": "Sanchez"},  {"firstName": "Morty", "lastName": "Smith"}]---input map ((item) -> item.firstName ++ " " ++ item.lastName)

Example 12: Modifying Nested Structure

Input:

{  "products": [    {"name": "Laptop", "specs": {"ram": 16}},    {"name": "Phone", "specs": {"ram": 4}}  ]}

Output:

[  {"name": "Laptop", "ram": 16},  {"name": "Phone", "ram": 4}]

Script:

%dw 2.0output application/jsonvar input = {  "products": [    {"name": "Laptop", "specs": {"ram": 16}},    {"name": "Phone", "specs": {"ram": 4}}  ]}---input.products map ((item) -> {  name: item.name,  ram: item.specs.ram})

Example 13: Merging and Formatting Fields

Input:

[  {"username": "user01", "domain": "example.com"},  {"username": "user02", "domain": "example.com"}]

Output:

[  "user01@example.com",  "user02@example.com"]

Script:

%dw 2.0output application/jsonvar input = [  {"username": "user01", "domain": "example.com"},  {"username": "user02", "domain": "example.com"}]---input map ((item) -> item.username ++ "@" ++ item.domain)

Example 14: Flattening Nested Arrays

Input:

[  {"tags": ["red", "blue"]},  {"tags": ["green", "yellow"]}]

Output:

["red", "blue", "green", "yellow"]

Script:

%dw 2.0output application/jsonvar input = [  {"tags": ["red", "blue"]},  {"tags": ["green", "yellow"]}]---input flatMap ((item) -> item.tags)

Example 15: Data Transformation with Default Values

Input:

[  {"product": "Shirt", "stock": 0},  {"product": "Shoes", "stock": 15}]

Output:

[  {"product": "Shirt", "availability": "Out of stock"},  {"product": "Shoes", "availability": "In stock"}]

Script:

%dw 2.0output application/jsonvar input = [  {"product": "Shirt", "stock": 0},  {"product": "Shoes", "stock": 15}]---input map ((item) -> {  product: item.product,  availability: if (item.stock > 0) "In stock" else "Out of stock"})

These examples demonstrate the versatility of the DataWeave map function for various data transformation tasks.

]]>
Post a Comment (0)
Previous Post Next Post