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:
icon png

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

DataWeave Code:
icon png

%dw 2.0
<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(<Specify the predefined function>,<define the
payload/input>)

1. UPPER

icon png

Code:

%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)

icon png

Output:

{
     "NAME":"Abhishek",
     "LOCATION":[
          {
               "COUNTRY":"INDIA"
          }
     ],
     "INFO":{
          "TYPE":"Blog",
          "TOPIC":"dataweave"
     }
}

img

2. lower

icon png

Code:

%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)

icon png

Output:

{
     "name":"Abhishek",
     "location":[
          {
               "country":"INDIA"
          }
     ],
     "info":{
          "type":"Blog",
          "topic":"dataweave"
     }
}

img

3. Capitalize

icon png

Code:

%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)

icon png

Output:

{
     "Name":"Abhishek",
     "Location":[
          {
               "Country":"INDIA"
          }
     ],
     "info":{
          "Type":"Blog",
          "Topic":"dataweave"
     }
}

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

icon png
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.
image

Create Meaningful Experiences for employees

Our power of choice is untrammelled and when nothing prevents our able to do what we like best every pleasure is to be welcomed and occur that pleasures have to be repudiated.

Post a comment

Your email address will not be published.