1. map (Transform elements in an array or object)
Map an array of numbers to their squares:
- Input:
[1, 2, 3, 4] - Output:
[1, 4, 9, 16] - Dataweave
- %dw 2.0output application/json---payload map ($*$)
Convert an array of strings to uppercase:
- Input:
["apple", "banana", "cherry"] - Output:
["APPLE", "BANANA", "CHERRY"]
- Input:
Prefix all keys in an object with "key_":
- Input:
{"name": "John", "age": 25} - Output:
{"key_name": "John", "key_age": 25}
- Input:
Map an array of objects to only include specific fields:
- Input:
[ {"name": "John", "age": 25, "city": "New York"}, {"name": "Jane", "age": 30, "city": "Los Angeles"} ] - Output:
[{"name": "John", "city": "New York"}, {"name": "Jane", "city": "Los Angeles"}]
- Input:
Add 10 to each number in an array:
- Input:
[1, 2, 3, 4, 5] - Output:
[11, 12, 13, 14, 15]
- Input:
Multiply each value in an array by its index:
- Input:
[10, 20, 30, 40] - Output:
[0, 20, 60, 120]
- Input:
Concatenate a fixed string to each element in an array:
- Input:
["John", "Jane", "Jake"] - Output:
["Hello John", "Hello Jane", "Hello Jake"]
- Input:
Replace null values in an array with "default":
- Input:
[1, null, 3, null, 5] - Output:
[1, "default", 3, "default", 5]
- Input:
Map an array of booleans to their string equivalents:
- Input:
[true, false, true] - Output:
["true", "false", "true"]
- Input:
Convert an array of dates to formatted strings:
- Input:
["2025-01-01", "2025-01-02"] - Output:
["2025-01-01T00:00:00Z", "2025-01-02T00:00:00Z"]
- Input:
2. filter (Filter elements from an array or object)
Filter even numbers from an array:
- Input:
[1, 2, 3, 4, 5, 6] - Output:
[2, 4, 6]
- Input:
Remove null values from an array:
- Input:
[1, null, 3, null, 5] - Output:
[1, 3, 5]
- Input:
Filter strings that start with "A":
- Input:
["Apple", "Banana", "Avocado", "Grapes"] - Output:
["Apple", "Avocado"]
- Input:
Keep only objects with a specific field value:
- Input:
[ {"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "Jake", "age": 25} ] - Output:
[{"name": "John", "age": 25}, {"name": "Jake", "age": 25}]
- Input:
Exclude negative numbers from an array:
- Input:
[5, -1, 7, -9, 3] - Output:
[5, 7, 3]
- Input:
Filter out duplicate values:
- Input:
[1, 2, 2, 3, 4, 4] - Output:
[1, 2, 3, 4]
- Input:
Retain only keys with values greater than 50:
- Input:
{"a": 10, "b": 60, "c": 30, "d": 70} - Output:
{"b": 60, "d": 70}
- Input:
Exclude elements containing specific substrings:
- Input:
["apple", "banana", "avocado", "grape"] - Output:
["banana", "grape"]
- Input:
Keep elements with length greater than 3:
- Input:
["a", "bb", "ccc", "dddd"] - Output:
["ccc", "dddd"]
- Input:
Retain objects where a nested value meets a condition:
- Input:
[ {"name": "John", "address": {"city": "New York"}}, {"name": "Jane", "address": {"city": "Los Angeles"}} ] - Output:
[{"name": "John", "address": {"city": "New York"}}]
- Input:
3. pluck (Extract key-value pairs)
Extract all keys from an object:
- Input:
{"name": "John", "age": 25, "city": "New York"} - Output:
["name", "age", "city"]
- Input:
Extract all values from an object:
- Input:
{"name": "John", "age": 25, "city": "New York"} - Output:
["John", 25, "New York"]
- Input:
Convert an object to an array of
[key, value]pairs:- Input:
{"name": "John", "age": 25, "city": "New York"} - Output:
[["name", "John"], ["age", 25], ["city", "New York"]]
- Input:
Add an index to each key in an object:
- Input:
{"name": "John", "age": 25} - Output:
[[0, "name", "John"], [1, "age", 25]]
- Input:
Generate a new object with modified keys:
- Input:
{"name": "John", "age": 25} - Output:
{"key_name": "John", "key_age": 25}
- Input:
4. reduce (Aggregate values)
Sum all numbers in an array:
- Input:
[1, 2, 3, 4, 5] - Output:
15
- Input:
Concatenate an array of strings:
- Input:
["hello", "world", "dataweave"] - Output:
"hello world dataweave"
- Input:
Find the maximum number in an array:
- Input:
[3, 5, 7, 2, 8] - Output:
8
- Input:
Compute the product of all numbers:
- Input:
[2, 3, 4] - Output:
24
- Input:
Count occurrences of each word in an array:
- Input:
["apple", "banana", "apple", "orange", "banana", "apple"] - Output:
{"apple": 3, "banana": 2, "orange": 1}
- Input:
5. groupBy (Group elements by a key or criteria)
Group transactions by customer ID:
- Input:
[ {"customerId": "A123", "amount": 100}, {"customerId": "A123", "amount": 200}, {"customerId": "B456", "amount": 150} ] - Output:
{"A123": [{"customerId": "A123", "amount": 100}, {"customerId": "A123", "amount": 200}], "B456": [{"customerId": "B456", "amount": 150}]}
- Input:
Group numbers by odd/even:
- Input:
[1, 2, 3, 4, 5, 6] - Output:
{"odd": [1, 3, 5], "even": [2, 4, 6]}
- Input:
Group objects by a specific property:
- Input:
[ {"name": "John", "age": 25, "city": "New York"}, {"name": "Jane", "age": 30, "city": "Los Angeles"}, {"name": "Jake", "age": 25, "city": "Chicago"} ] - Output:
{"25": [{"name": "John", "age": 25, "city": "New York"}, {"name": "Jake", "age": 25, "city": "Chicago"}], "30": [{"name": "Jane", "age": 30, "city": "Los Angeles"}]}
- Input:
6. join (Combine arrays or objects)
- Join two arrays of objects by a shared key:
- Input:
- Array 1:
[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}] - Array 2:
[{"id": 1, "age": 25}, {"id": 2, "age": 30}]
- Array 1:
- Output:
[{"id": 1, "name": "John", "age": 25}, {"id": 2, "name": "Jane", "age": 30}]
- Combine arrays into a single array of objects:
- Input:
- Array 1:
[1, 2, 3] - Array 2:
[4, 5, 6]
- Array 1:
- Output:
[{1, 4}, {2, 5}, {3, 6}]
- Merge two objects into one:
- Input:
- Object 1:
{"name": "John", "age": 25} - Object 2:
{"city": "New York", "job": "Engineer"}
- Object 1:
- Output:
{"name": "John", "age": 25, "city": "New York", "job": "Engineer"}
- Perform an inner join on two datasets:
- Input:
- Dataset 1:
[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}] - Dataset 2:
[{"id": 1, "age": 25}, {"id": 3, "age": 30}]
- Dataset 1:
- Output:
[{"id": 1, "name": "John", "age": 25}]
- Create a left join of users and orders:
- Input:
- Users:
[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}] - Orders:
[{"userId": 1, "order": "Laptop"}, {"userId": 2, "order": "Phone"}]
- Users:
- Output:
[{"id": 1, "name": "John", "order": "Laptop"}, {"id": 2, "name": "Jane", "order": "Phone"}]
- Combine multiple objects with overlapping keys:
- Input:
- Object 1:
{"name": "John", "age": 25} - Object 2:
{"age": 30, "city": "New York"}
- Object 1:
- Output:
{"name": "John", "age": 30, "city": "New York"}
- Create a new array by combining elements from two arrays:
- Input:
- Array 1:
[1, 2, 3] - Array 2:
[4, 5, 6]
- Array 1:
- Output:
[1, 2, 3, 4, 5, 6]
7. distinctBy (Remove duplicates)
- Remove duplicate numbers from an array:
- Input:
[1, 2, 2, 3, 4, 4] - Output:
[1, 2, 3, 4]
- Keep unique objects based on a specific property:
- Input:
[ {"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "John", "age": 25} ] - Output:
[{"name": "John", "age": 25}, {"name": "Jane", "age": 30}]
- Create a unique list of words from a paragraph:
- Input:
"apple banana apple orange banana grape" - Output:
["apple", "banana", "orange", "grape"]
- Remove duplicate strings ignoring case:
- Input:
["apple", "Apple", "banana", "Banana"] - Output:
["apple", "banana"]
- Deduplicate based on nested field values:
- Input:
[ {"id": 1, "address": {"city": "New York"}}, {"id": 2, "address": {"city": "Los Angeles"}}, {"id": 1, "address": {"city": "New York"}} ] - Output:
[{"id": 1, "address": {"city": "New York"}}, {"id": 2, "address": {"city": "Los Angeles"}}]
- Remove duplicates using a custom condition:
- Input:
[ {"id": 1, "name": "John"}, {"id": 1, "name": "John"}, {"id": 2, "name": "Jane"} ] - Output:
[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}]
- Deduplicate while retaining the first occurrence:
- Input:
[1, 2, 2, 3, 4, 4] - Output:
[1, 2, 3, 4]
- Filter unique values using a key hash:
- Input:
[ {"id": 1, "name": "John"}, {"id": 1, "name": "Jane"}, {"id": 2, "name": "Jake"} ] - Output:
[{"id": 1, "name": "John"}, {"id": 2, "name": "Jake"}]
- Remove duplicates from merged arrays:
- Input:
[1, 2, 3]and[2, 3, 4] - Output:
[1, 2, 3, 4]
- Remove duplicates from a set of objects:
- Input:
[ {"id": 1, "value": "apple"}, {"id": 1, "value": "apple"}, {"id": 2, "value": "banana"} ] - Output:
[{"id": 1, "value": "apple"}, {"id": 2, "value": "banana"}]
8. groupBy (Group elements by a key or criteria)
- Group transactions by customer ID:
- Input:
[ {"customerId": "A123", "amount": 100}, {"customerId": "A123", "amount": 200}, {"customerId": "B456", "amount": 150} ] - Output:
{"A123": [{"customerId": "A123", "amount": 100}, {"customerId": "A123", "amount": 200}], "B456": [{"customerId": "B456", "amount": 150}]}
- Group numbers by odd/even:
- Input:
[1, 2, 3, 4, 5, 6] - Output:
{"odd": [1, 3, 5], "even": [2, 4, 6]}
- Group objects by a specific property:
- Input:
[ {"name": "John", "age": 25, "city": "New York"}, {"name": "Jane", "age": 30, "city": "Los Angeles"}, {"name": "Jake", "age": 25, "city": "Chicago"} ] - Output:
{"25": [{"name": "John", "age": 25, "city": "New York"}, {"name": "Jake", "age": 25, "city": "Chicago"}], "30": [{"name": "Jane", "age": 30, "city": "Los Angeles"}]}
- Group strings by the first letter:
- Input:
["apple", "banana", "avocado", "grape"] - Output:
{"a": ["apple", "avocado"], "b": ["banana"], "g": ["grape"]}
- Group objects by a nested field:
- Input:
[ {"id": 1, "info": {"city": "New York"}}, {"id": 2, "info": {"city": "Los Angeles"}}, {"id": 3, "info": {"city": "New York"}} ] - Output:
{"New York": [{"id": 1, "info": {"city": "New York"}}, {"id": 3, "info": {"city": "New York"}}], "Los Angeles": [{"id": 2, "info": {"city": "Los Angeles"}}]}
- Group dates by year:
- Input:
["2023-01-01", "2024-01-01", "2023-05-01"] - Output:
{"2023": ["2023-01-01", "2023-05-01"], "2024": ["2024-01-01"]}
- Group transactions by date:
- Input:
[ {"date": "2023-01-01", "amount": 100}, {"date": "2023-01-02", "amount": 200}, {"date": "2023-01-01", "amount": 150} ] - Output:
{"2023-01-01": [{"date": "2023-01-01", "amount": 100}, {"date": "2023-01-01", "amount": 150}], "2023-01-02": [{"date": "2023-01-02", "amount": 200}]}
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment