DigitalBuzz Logo
MuleSoft

Custom Function To Transform Only The Keys Using Dataweave In Mule 4

Abhishek Bathwal
Posted By
Abhishek Bathwal
Technical Guide
6 min read
Custom Function To Transform Only The Keys Using Dataweave In Mule 4

This blog will showcase how we can transform only keys out of a key-value pair of type String, Array of Objects, or Objects in DataWeave using DataWeave predefined functions like UPPER, lower, Capitalize, etc.

Input:

input.json
json
{
  "name": "Abhishek",
  "location": [
    {
      "country": "INDIA"
    }
  ],
  "info": {
    "type": "Blog",
    "Topic": "dataweave"
  }
}

DataWeave Code:

Generic DataWeave 2.0 Function
dataweave
%dw 2.0
<"text-amber-300">class="text-sky-400 font-semibold">import the correct module if applicable>
output application/json
fun trans(frmt,inpt) = inpt mapObject (val, key, ind) -> {
  ((frmt(key)): val) if (typeOf(val) ~= "String"),
  ((frmt(key)): (val map (($ pluck (value, key, index)
    -> (frmt(key)): value)
    reduce ($$ ++ $)))) if (typeOf(val) ~= "Array"),
  ((frmt(key)): (val mapObject (val1, key1, ind1) ->
    ((frmt(key1)): val1)))
    if (typeOf(val) ~= "Object")
}
---
trans(<"text-amber-300">class="text-sky-400 font-semibold">Specify the predefined function>,<"text-amber-300">class="text-sky-400 font-semibold">define the payload/input>)

1UPPER

DataWeave — trans(upper, payload)
dataweave
%dw 2.0
output application/json
fun trans(frmt,inpt) = inpt mapObject (val, key, ind) -> {
  ((frmt(key)): val) if (typeOf(val) ~= "String"),
  ((frmt(key)): (val map (($ pluck (value, key, index)
    -> (frmt(key)): value)
    reduce ($$ ++ $)))) if (typeOf(val) ~= "Array"),
  ((frmt(key)): (val mapObject (val1, key1, ind1) ->
    ((frmt(key1)): val1)))
    if (typeOf(val) ~= "Object")
}
---
trans(upper, payload)

Output:

Output (UPPER)
json
{
  "NAME": "Abhishek",
  "LOCATION": [
    {
      "COUNTRY": "INDIA"
    }
  ],
  "INFO": {
    "TYPE": "Blog",
    "TOPIC": "dataweave"
  }
}
DataWeave Playground showing UPPER transformation
Figure 1: Executing trans(upper, payload) in DataWeave Playground

2lower

DataWeave — trans(lower, payload)
dataweave
%dw 2.0
output application/json
fun trans(frmt,inpt) = inpt mapObject (val, key, ind) -> {
  ((frmt(key)): val) if (typeOf(val) ~= "String"),
  ((frmt(key)): (val map (($ pluck (value, key, index)
    -> (frmt(key)): value)
    reduce ($$ ++ $)))) if (typeOf(val) ~= "Array"),
  ((frmt(key)): (val mapObject (val1, key1, ind1) ->
    ((frmt(key1)): val1)))
    if (typeOf(val) ~= "Object")
}
---
trans(lower, payload)

Output:

Output (lower)
json
{
  "name": "Abhishek",
  "location": [
    {
      "country": "INDIA"
    }
  ],
  "info": {
    "type": "Blog",
    "topic": "dataweave"
  }
}
DataWeave Playground showing lower transformation
Figure 2: Executing trans(lower, payload) in DataWeave Playground

3Capitalize

DataWeave — trans(capitalize, payload)
dataweave
%dw 2.0
import * from dw::core::Strings
output application/json
fun trans(frmt,inpt) = inpt mapObject (val, key, ind) -> {
  ((frmt(key)): val) if (typeOf(val) ~= "String"),
  ((frmt(key)): (val map (($ pluck (value, key, index)
    -> (frmt(key)): value)
    reduce ($$ ++ $)))) if (typeOf(val) ~= "Array"),
  ((frmt(key)): (val mapObject (val1, key1, ind1) ->
    ((frmt(key1)): val1)))
    if (typeOf(val) ~= "Object")
}
---
trans(capitalize, payload)

Output:

Output (Capitalize)
json
{
  "Name": "Abhishek",
  "Location": [
    {
      "Country": "INDIA"
    }
  ],
  "info": {
    "Type": "Blog",
    "Topic": "dataweave"
  }
}
DataWeave Playground showing Capitalize transformation
Figure 3: Executing trans(capitalize, payload) with dw::core::Strings

Note:

i) Tried the code on Dataweave Playground and Dataweave Version 2.4.

ii) This is one way of handling the explained scenario, there can be other possible solutions depending on the requirements.

Quote

“Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.”

Abhishek Bathwal
About the Author

Abhishek Bathwal

Integration and API Specialist at DigitalBuzz Software, specializing in advanced DataWeave transformations, RAML design, and enterprise middleware architecture.

Share this article