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

Author: Abhishek Bathwal

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:

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

DataWeave Code:

%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

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)

Output:

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

2. lower

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)

Output:

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

3. Capitalize

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)

Output:

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

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.

********* Happy Learning *********

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.