---
schema_version: "1"
id: 027b315fe8768187cdf1519a4ac695e1eea2a0c888ca6500b587f5d809256643
title: Rename JSON Keys
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-cookbook-rename-keys.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-rename-keys
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 060e0c23ad4968ec4f28aa048fdbfbcfd9b37a3eb8e86f90d6deace6e137ae2f
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Rename JSON Keys

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

This DataWeave example renames some keys in a JSON object, while retaining the names of all others
in the output.

The example uses these functions:

- `mapObject` to go through the `key:value` pairs in a JSON object.

- `if` by itself to determine when to change the name of a key.

- `if` with `and` to retain the name of all keys except the two with new names.

- `as` to coerce the type of the keys into a String.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
---
payload.flights map (flight) -> {
    (flight mapObject (value, key) -> {
        (emptySeats: value) if(key as String == 'availableSeats'),
        (airline: value) if(key as String == 'airlineName'),
        ((key):value) if(key as String !='availableSeats' and key as String != 'airlineName')
    })
}
```

Input JSON Payload:

```json
{
  "flights": [
    {
      "availableSeats": 45,
      "airlineName": "Ryan Air",
      "aircraftBrand": "Boeing",
      "aircraftType": "737",
      "departureDate": "12/14/2017",
      "origin": "BCN",
      "destination": "FCO"
    },
    {
      "availableSeats": 15,
      "airlineName": "Ryan Air",
      "aircraftBrand": "Boeing",
      "aircraftType": "747",
      "departureDate": "08/03/2017",
      "origin": "FCO",
      "destination": "DFW"
    }
  ]
}
```

Output JSON:

```json
[
  {
    "emptySeats": 45,
    "airline": "Ryan Air",
    "aircraftBrand": "Boeing",
    "aircraftType": "737",
    "departureDate": "12/14/2017",
    "origin": "BCN",
    "destination": "FCO"
  },
  {
    "emptySeats": 15,
    "airline": "Ryan Air",
    "aircraftBrand": "Boeing",
    "aircraftType": "747",
    "departureDate": "08/03/2017",
    "origin": "FCO",
    "destination": "DFW"
  }
]
```

A more maintainable way to produce the same output uses DataWeave pattern matching.

The example uses the following functions and statements:

- `mapObject` to go through the `key:value` pairs in a JSON object.

- `match` on each key in the input.

- `case` statements to change the name of matching keys in the input.

- `else` statement to retain the name of keys that do not require a name change.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
fun renameKey(key: Key) = key match {
    case "availableSeats" -> "emptySeats"
    case "airlineName" -> "airline"
    else -> (key)
}
---
payload.flights map (flight) ->
flight mapObject (value, key) -> {
    (renameKey(key)) : value
}
```

Input JSON Payload:

```json
{
  "flights": [
    {
      "availableSeats": 45,
      "airlineName": "Ryan Air",
      "aircraftBrand": "Boeing",
      "aircraftType": "737",
      "departureDate": "12/14/2017",
      "origin": "BCN",
      "destination": "FCO"
    },
    {
      "availableSeats": 15,
      "airlineName": "Ryan Air",
      "aircraftBrand": "Boeing",
      "aircraftType": "747",
      "departureDate": "08/03/2017",
      "origin": "FCO",
      "destination": "DFW"
    }
  ]
}
```

Output JSON:

```json
[
  {
    "emptySeats": 45,
    "airline": "Ryan Air",
    "aircraftBrand": "Boeing",
    "aircraftType": "737",
    "departureDate": "12/14/2017",
    "origin": "BCN",
    "destination": "FCO"
  },
  {
    "emptySeats": 15,
    "airline": "Ryan Air",
    "aircraftBrand": "Boeing",
    "aircraftType": "747",
    "departureDate": "08/03/2017",
    "origin": "FCO",
    "destination": "DFW"
  }
]
```

## Related Examples

- [Output a Field When Present](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-output-a-field-when-present)

- [Dynamically Map Based on a Definition](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-map-based-on-an-external-definition)

- [Pass Functions as Arguments](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-pass-functions-as-arguments)

## See Also

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

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