---
schema_version: "1"
id: 7baeec3abc5e6a39e539029c620f1dad83e1ae7ca8b129c28ddeb2dfedd61e99
title: Regroup Fields
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-cookbook-regroup-fields.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-regroup-fields
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 60019afeaed376abad26f0520672c9c8ebdfcdd81aeaf2f4054c925eca7f95aa
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
keywords: studio, anypoint, transform, transformer, format, xml, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, groupby, mapobject
---

# Regroup Fields

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

These DataWeave examples take input that is grouped under one field and transform it into a new
structure that groups data under another field.

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 Script:

```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 Payload:

```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:

```json
{
  "classrooms": {
    "class": {
      "name": "DW",
      "teachers": {
        "teacher": {
          "name": "Mariano",
          "lastName": "De Achaval"
        },
        "teacher": {
          "name": "Emiliano",
          "lastName": "Lesende"
        }
      }
    },
    "class": {
      "name": "Scala",
      "teachers": {
        "teacher": {
          "name": "Leandro",
          "lastName": "Shokida"
        }
      }
    }
  }
}
```

## 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:

```dataweave
%dw 2.0
output application/json
---
{
    "langs" :
        payload.langs groupBy $.language
            mapObject ((nameGroup, language) -> {
                 (language): {
                    "attendees" : nameGroup map {
                        name: $.name
                    }
                 }
            })
}
```

Input JSON Payload:

```json
{
  "langs": [
    {
      "name": "Alex",
      "language": "Java"
    },
    {
      "name": "Kris",
      "language": "Scala"
    },
    {
      "name": "Jorge",
      "language": "Java"
    }
  ]
}
```

Output JSON:

```json
{
  "langs": {
    "Java": {
      "attendees": [
        {
          "name": "Alex"
        },
        {
          "name": "Jorge"
        }
      ]
    },
    "Scala": {
      "attendees": [
        {
          "name": "Kris"
        }
      ]
    }
  }
}
```

## Related Examples

- [Map Data](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-map)

- [Reference Multiple Inputs](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-reference-multiple-inputs)

- [Define a Function that Flattens Data in a List](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-define-function-to-flatten-list)

- [Zip Arrays Together](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-zip-arrays-together)

## See Also

- [DataWeave Types](https://docs.mulesoft.com/dataweave/2.9/dataweave-types)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.9/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook)
