Introduction to DataWeave in MuleSoft: Working with Objects and Arrays

Overview

DataWeave is the powerful transformation language used in MuleSoft to convert data from one format to another. Whether you are working with JSON, XML, or other data types, understanding objects and arrays is the foundation of writing effective DataWeave scripts.

In this article, we will start with the basics of DataWeave and explain how objects and arrays work using simple and practical examples. You will learn how to read, create, and modify objects and arrays, and how they are commonly used in real integration scenarios.

Understanding

    • Basic Structure of a DataWeave Script
      Every DataWeave script has three core sections:

      • Header section
      • Body separator (—)
      • Body (transformation logic)
        %dw 2.0
        output application/json
        ---
        {
          message: "Welcome to DataWeave"
        }
      • %dw 2.0 → DataWeave version
      • output application/json → Output format
      • --- → Separates header and body
      • Body → Defines the output structure
    • Declaring Variables in the Header Section

      The header section can also be used to declare variables, objects, and arrays using the var keyword. These declared values can then be accessed in the body. This improves readability, reusability, and clean code structure.

      %dw 2.0
      output application/json
      var employee = {
        id: "EMP1001",
        name: "Rajesh",
        experience: 6,
        role: "Integration Developer"
      }
      ---
      {
        employeeId: employee.id,
        employeeName: employee.name,
        employeeRole: employee.role
      }
      • The object employee is declared in the header.
      • employee.id, employee.name, and employee.role are accessed in the body.
      • This approach avoids hardcoding values multiple times.
    • Nested Objects (Object Inside an Object)
      Objects can contain other objects, which is very common in real API responses.
      %dw 2.0
      output application/json
      var user = {
        id: "U101",
        profile: {
          firstName: "Ravi",
          lastName: "Kumar",
          contact: {
            email: "ravi@test.com",
            phone: "9999999999"
          }
        }
      }
      ---
      {
        userName: user.profile.firstName ++ " " ++ user.profile.lastName,
        userEmail: user.profile.contact.email
      }
      • profile and contact are nested objects.
      • Dot notation is used to access nested values.
      • Nested structures help represent complex data clearly.
    • Understanding Arrays in DataWeave
      An array is an ordered collection of values. Arrays are used when multiple records of the same type need to be stored together.
      %dw 2.0
      output application/json
      ---
      ["MuleSoft", "DataWeave", "API"]
    • Declaring an Array in the Header and Accessing It in the Body
       
      %dw 2.0
      output application/json
      var skills = ["MuleSoft", "Java", "DataWeave"]
      ---
      {
        primarySkill: skills[0],
        allSkills: skills
      }
      • Array index starts from 0.
      • skills[0] returns the first element.
      • Declaring arrays in the header keeps logic clean
    • Array of Objects
      An array of objects is one of the most frequently used structures in MuleSoft integrations.
       
      %dw 2.0
      output application/json
      var employees = [
        { id: 1, name: "Anita", department: "HR" },
        { id: 2, name: "Rahul", department: "IT" },
        { id: 3, name: "Suman", department: "Finance" }
      ]
      ---
      employees
      • Each array element is an object.
      • This structure matches most REST API responses.
    • Accessing Data from an Array of Objects
       
      %dw 2.0
      output application/json
      var employees = [
        { id: 1, name: "Anita", department: "HR" },
        { id: 2, name: "Rahul", department: "IT" }
      ]
      ---
      {
        firstEmployeeName: employees[0].name,
        secondEmployeeDepartment: employees[1].department
      }
      • Combine array index and dot notation.
      • First select the array element, then access object fields.
    • Looping Through an Array of Objects using map()
       
      %dw 2.0
      output application/json
      var employees = [
        { name: "Amit", age: 28 },
        { name: "Neha", age: 32 }
      ]
      ---
      employees map (emp) -> {
        employeeName: emp.name,
        employeeAge: emp.age
      }
      • map iterates through each object.
      • A new array is returned.
      • Used heavily for transformations
    • Object Containing an Array
      Sometimes an object includes an array as one of its fields.
       
      %dw 2.0
      output application/json
      var company = {
        name: "TechSoft",
        locations: ["Bangalore", "Hyderabad", "Pune"]
      }
      ---
      {
        companyName: company.name,
        availableLocations: company.locations
      }
      • locations is an array inside an object.
      • Very common in configuration and master data.
    • Object Containing an Array of Objects
       
      %dw 2.0
      output application/json
      var organization = {
        orgName: "HarmoniGate",
        employees: [
          { id: "E1", name: "Ravi", role: "Developer" },
          { id: "E2", name: "Sneha", role: "Tester" }
        ]
      }
      ---
      {
        organization: organization.orgName,
        employeeList: organization.employees map (emp) -> {
          employeeId: emp.id,
          employeeName: emp.name
        }
      }
      • Object contains an array of objects.
      • map is used to reshape employee data.

You can try this in the DataWeave Playground. Stay tuned, we’ll be publishing more DataWeave articles soon.

Leave a Comment