Contact Free trial Login

Rename JSON Keys

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

It 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.

%dw 2.0
output application/json
--- 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')
  "airlineName":"Ryan Air",
  "airlineName":"Ryan Air",
    "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"

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.