Wednesday 13 March 2024

How to convert all the keys in an object to uppercase ? in MuleSoft 117

 How to convert all the keys in an object to uppercase ? in MuleSoft

There are two primary methods to convert all keys in an object to uppercase within MuleSoft 4:

1. Using DataWeave:

DataWeave offers a powerful approach for manipulating objects and their properties. Here's how to achieve the conversion:

%dw 2.0
output = message.payload mapObject ((key, value) -> { (upper(key)): value })


  • The mapObject function iterates over each key-value pair in the original payload object.

  • Within the function, a new object is constructed.

  • The key is converted to uppercase using the upper function.

  • The original value is retained.

  • The final output variable holds the new object with all keys in uppercase.

2. Using MEL (Message Expression Language):

While less flexible than DataWeave, MEL can be used for this specific task. Here's the approach:


#[(payload map { (key: k, value: v) -> [MULE_UTIL:upper(k)]: v })]


  • The expression utilizes a custom function using MULE_UTIL:upper to convert the key to uppercase within the anonymous function.

  • Similar to DataWeave, a new map is created, iterating over each key-value pair.

  • The uppercase key becomes the new key in the resulting map.

Choosing the Right Approach:

  • DataWeave: This is the recommended method due to its readability, maintainability, and broader data manipulation capabilities.

  • MEL: While functional for this specific scenario, MEL can become cumbersome for complex data transformations.

Additional Notes:

  • Ensure the payload is a valid JSON object before applying these methods.

  • Error handling can be incorporated within DataWeave or MEL to handle cases where the payload might not be in the expected format.

Here are some helpful resources for further exploration:

No comments:

Post a Comment

Note: only a member of this blog may post a comment.