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
<"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
%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"
}
}
2lower
%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"
}
}
3Capitalize
%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.”

Abhishek Bathwal
Integration and API Specialist at DigitalBuzz Software, specializing in advanced DataWeave transformations, RAML design, and enterprise middleware architecture.
Related Articles
All ArticlesPassing at least one mandatory Query Params out of many defined in RAML
Learn how to handle mandatory query parameters in RAML when an API defines multiple query parameters.
Connecting Amazon Redshift Database using MuleSoft DB Connector
Learn how to connect Amazon Redshift with MuleSoft Database Connector using JDBC drivers.

