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:

{
"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.

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.

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.