Regroup Fields

DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

These DataWeave examples take input that is grouped under one field and transform it into a new structure that groups data under another field. Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

Both examples use these functions:

  • groupBy to organize the fields by subject

  • mapObject and map to map the fields from the input to the new hierarchy.

Example: XML to JSON

DataWeave
%dw 2.0
output application/json
---
classrooms: payload..*teacher groupBy $.subject mapObject ((teacherGroup, subject) -> {
   class: {
     name: subject,
     teachers: { (teacherGroup map {
       teacher:{
           name: $.name,
           lastName: $.lastName
       }
     })
   }
  }
})
Input XML
<school>
    <teachers>
        <teacher>
            <name>Mariano</name>
            <lastName>De Achaval</lastName>
            <subject>DW</subject>
        </teacher>
        <teacher>
            <name>Emiliano</name>
            <lastName>Lesende</lastName>
            <subject>DW</subject>
        </teacher>
        <teacher>
            <name>Leandro</name>
            <lastName>Shokida</lastName>
            <subject>Scala</subject>
        </teacher>
    </teachers>
</school>
Output JSON
{
  "classrooms": {
    "class": {
      "name": "Scala",
      "teachers": {
        "teacher": {
          "name": "Leandro",
          "lastName": "Shokida"
        }
      }
    },
    "class": {
      "name": "DW",
      "teachers": {
        "teacher": {
          "name": "Mariano",
          "lastName": "De Achaval"
        },
        "teacher": {
          "name": "Emiliano",
          "lastName": "Lesende"
        }
      }
    }
  }
}

Example: JSON to JSON

This DataWeave example changes the hierarchy of a JSON object. The output groups fields by language and adds a new element, attendees, that contains the names of attendees for each course.

DataWeave Script:
%dw 2.0
output application/json
---
{
    "langs" :
        payload.langs groupBy $.language
            mapObject ((nameGroup, language) -> {
                 (language): {
                    "attendees" : nameGroup map {
                        name: $.name
                    }
                 }
            })
}
Input JSON Payload:
{
  "langs": [
    {
      "name": "Alex",
      "language": "Java"
    },
    {
      "name": "Kris",
      "language": "Scala"
    },
    {
      "name": "Jorge",
      "language": "Java"
    }
  ]
}
Output JSON:
{
  "langs": {
    "Java": {
      "attendees": [
        {
          "name": "Alex"
        },
        {
          "name": "Jorge"
        }
      ]
    },
    "Scala": {
      "attendees": [
        {
          "name": "Kris"
        }
      ]
    }
  }
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub