Skip to main content

Mulesoft Dataweave Practice Questions-2025

1. map (Transform elements in an array or object)

  1. Map an array of numbers to their squares:

    • Input: [1, 2, 3, 4]
    • Output: [1, 4, 9, 16]
    • Dataweave
    • %dw 2.0
      output application/json
      ---
      payload map ($*$)
  2. Convert an array of strings to uppercase:

    • Input: ["apple", "banana", "cherry"]
    • Output: ["APPLE", "BANANA", "CHERRY"]
  3. Prefix all keys in an object with "key_":

    • Input: {"name": "John", "age": 25}
    • Output: {"key_name": "John", "key_age": 25}
  4. 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"}]
  5. Add 10 to each number in an array:

    • Input: [1, 2, 3, 4, 5]
    • Output: [11, 12, 13, 14, 15]
  6. Multiply each value in an array by its index:

    • Input: [10, 20, 30, 40]
    • Output: [0, 20, 60, 120]
  7. Concatenate a fixed string to each element in an array:

    • Input: ["John", "Jane", "Jake"]
    • Output: ["Hello John", "Hello Jane", "Hello Jake"]
  8. Replace null values in an array with "default":

    • Input: [1, null, 3, null, 5]
    • Output: [1, "default", 3, "default", 5]
  9. Map an array of booleans to their string equivalents:

    • Input: [true, false, true]
    • Output: ["true", "false", "true"]
  10. 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"]

2. filter (Filter elements from an array or object)

  1. Filter even numbers from an array:

    • Input: [1, 2, 3, 4, 5, 6]
    • Output: [2, 4, 6]
  2. Remove null values from an array:

    • Input: [1, null, 3, null, 5]
    • Output: [1, 3, 5]
  3. Filter strings that start with "A":

    • Input: ["Apple", "Banana", "Avocado", "Grapes"]
    • Output: ["Apple", "Avocado"]
  4. 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}]
  5. Exclude negative numbers from an array:

    • Input: [5, -1, 7, -9, 3]
    • Output: [5, 7, 3]
  6. Filter out duplicate values:

    • Input: [1, 2, 2, 3, 4, 4]
    • Output: [1, 2, 3, 4]
  7. Retain only keys with values greater than 50:

    • Input: {"a": 10, "b": 60, "c": 30, "d": 70}
    • Output: {"b": 60, "d": 70}
  8. Exclude elements containing specific substrings:

    • Input: ["apple", "banana", "avocado", "grape"]
    • Output: ["banana", "grape"]
  9. Keep elements with length greater than 3:

    • Input: ["a", "bb", "ccc", "dddd"]
    • Output: ["ccc", "dddd"]
  10. 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"}}]

3. pluck (Extract key-value pairs)

  1. Extract all keys from an object:

    • Input: {"name": "John", "age": 25, "city": "New York"}
    • Output: ["name", "age", "city"]
  2. Extract all values from an object:

    • Input: {"name": "John", "age": 25, "city": "New York"}
    • Output: ["John", 25, "New York"]
  3. 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"]]
  4. Add an index to each key in an object:

    • Input: {"name": "John", "age": 25}
    • Output: [[0, "name", "John"], [1, "age", 25]]
  5. Generate a new object with modified keys:

    • Input: {"name": "John", "age": 25}
    • Output: {"key_name": "John", "key_age": 25}

4. reduce (Aggregate values)

  1. Sum all numbers in an array:

    • Input: [1, 2, 3, 4, 5]
    • Output: 15
  2. Concatenate an array of strings:

    • Input: ["hello", "world", "dataweave"]
    • Output: "hello world dataweave"
  3. Find the maximum number in an array:

    • Input: [3, 5, 7, 2, 8]
    • Output: 8
  4. Compute the product of all numbers:

    • Input: [2, 3, 4]
    • Output: 24
  5. Count occurrences of each word in an array:

    • Input: ["apple", "banana", "apple", "orange", "banana", "apple"]
    • Output: {"apple": 3, "banana": 2, "orange": 1}

5. groupBy (Group elements by a key or criteria)

  1. 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}]}
  2. Group numbers by odd/even:

    • Input: [1, 2, 3, 4, 5, 6]
    • Output: {"odd": [1, 3, 5], "even": [2, 4, 6]}
  3. 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"}]}

6. join (Combine arrays or objects)

  1. 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}]
  • Output: [{"id": 1, "name": "John", "age": 25}, {"id": 2, "name": "Jane", "age": 30}]
  1. Combine arrays into a single array of objects:
  • Input:
    • Array 1: [1, 2, 3]
    • Array 2: [4, 5, 6]
  • Output: [{1, 4}, {2, 5}, {3, 6}]
  1. Merge two objects into one:
  • Input:
    • Object 1: {"name": "John", "age": 25}
    • Object 2: {"city": "New York", "job": "Engineer"}
  • Output: {"name": "John", "age": 25, "city": "New York", "job": "Engineer"}
  1. 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}]
  • Output: [{"id": 1, "name": "John", "age": 25}]
  1. 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"}]
  • Output: [{"id": 1, "name": "John", "order": "Laptop"}, {"id": 2, "name": "Jane", "order": "Phone"}]
  1. Combine multiple objects with overlapping keys:
  • Input:
    • Object 1: {"name": "John", "age": 25}
    • Object 2: {"age": 30, "city": "New York"}
  • Output: {"name": "John", "age": 30, "city": "New York"}
  1. Create a new array by combining elements from two arrays:
  • Input:
    • Array 1: [1, 2, 3]
    • Array 2: [4, 5, 6]
  • Output: [1, 2, 3, 4, 5, 6]

7. distinctBy (Remove duplicates)

  1. Remove duplicate numbers from an array:
  • Input: [1, 2, 2, 3, 4, 4]
  • Output: [1, 2, 3, 4]
  1. 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}]
  1. Create a unique list of words from a paragraph:
  • Input: "apple banana apple orange banana grape"
  • Output: ["apple", "banana", "orange", "grape"]
  1. Remove duplicate strings ignoring case:
  • Input: ["apple", "Apple", "banana", "Banana"]
  • Output: ["apple", "banana"]
  1. 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"}}]
  1. 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"}]
  1. Deduplicate while retaining the first occurrence:
  • Input: [1, 2, 2, 3, 4, 4]
  • Output: [1, 2, 3, 4]
  1. 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"}]
  1. Remove duplicates from merged arrays:
  • Input: [1, 2, 3] and [2, 3, 4]
  • Output: [1, 2, 3, 4]
  1. 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)

  1. 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}]}
  1. Group numbers by odd/even:
  • Input: [1, 2, 3, 4, 5, 6]
  • Output: {"odd": [1, 3, 5], "even": [2, 4, 6]}
  1. 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"}]}
  1. Group strings by the first letter:
  • Input: ["apple", "banana", "avocado", "grape"]
  • Output: {"a": ["apple", "avocado"], "b": ["banana"], "g": ["grape"]}
  1. 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"}}]}
  1. 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"]}
  1. 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}]}

Comments

Popular posts from this blog

Dataweave My Practice

 https://www.caeliusconsulting.com/blogs/transforming-messages-with-dataweave/ Add 1 to each value in the array  [1,2,3,4,5]              Ans: %dw 2.0 output application/json --- payload map $+ 1  2) Get a list of  id s from:                [ { "id": 1, "name": "Archer" }, { "id": 2, "name": "Cyril" }, { "id": 3, "name": "Pam" } ] %dw 2.0 output application/json --- payload map $.id   ___________________________________________________________________________________ Example 1 : Using the input data, we'll produce a patterned output based on the given array. Input: [ { "name" : "Roger" }, { "name" : "Michael" }, { "name" : "Harris" } ] Output: [ { "user 1" : "Roger" }, { "user 2" : "Micheal" }, { "user 3" : "Harris...

Mule Interview Questions

### **1. Core MuleSoft Concepts** #### **What is MuleSoft, and what are its key components?** - **MuleSoft** is an integration platform that enables organizations to connect applications, data, and devices seamlessly. - **Key Components**:   - **Anypoint Platform**: The core platform for designing, building, and managing APIs and integrations.   - **Anypoint Studio**: The IDE for developing Mule applications.   - **Anypoint Exchange**: A repository for sharing APIs, templates, and connectors.   - **Runtime Engine**: Executes Mule applications.   - **API Manager**: Manages and secures APIs.   - **DataWeave**: Transformation language for data mapping. #### **Explain the API-led connectivity approach in MuleSoft.** - **API-led connectivity** is a method of connecting data and applications through reusable APIs. - It consists of three layers:   1. **System APIs**: Expose data from core systems.   2. **Process APIs**: Orchestrate data and business logi...