Contact Us 1-800-596-4880

Format According to Type

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, 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.

This DataWeave example applies changes to the keys in an object, depending on the type of their corresponding values. When the element is an array, the keys are pluralized. When it’s a number, they are set to camel case. In any other case they are capitalized.

This example uses:

  • mapObject to go through each element in the payload.

  • if to check that an element is of a given type.

  • camelize from the String library to apply a camel case format to strings (all words stringed together, using upper case letters to mark separations).

  • capitalize from the String library to apply a capitalized format to strings (all words are separate and start with an upper case letter).

  • pluralize from the String library to change singular words into plural.

DataWeave
%dw 2.0
import * from dw::core::Strings
output application/json
---
payload mapObject ((elementValue, elementKey) -> {
  (if (elementValue is Array)
      pluralize(elementKey)
    else if(elementValue is Number)
      camelize(elementKey)
    else capitalize(elementKey)) : elementValue
})
Input
{
  "VersionNo": 1.6,
  "StoreOfOrigin": "SFO",
  "Item":
    [
      {
        "ID":"34546315801",
        "DeliveryMethod":"AIR",
        "Quantity":8
      },
      {
        "ID":"56722087289",
        "Boxes": 3,
        "DeliveryMethod":"GROUND",
        "Quantity":2
      }
    ]
  }
Output
{
  "versionNo": 1.6,
  "Store Of Origin": "SFO",
  "Items": [
    {
      "ID": "34546315801",
      "DeliveryMethod": "AIR",
      "Quantity": 8
    },
    {
      "ID": "56722087289",
      "Boxes": 3,
      "DeliveryMethod": "GROUND",
      "Quantity": 2
    }
  ]
}