Gorodeckij Dimitrij
Gorodeckij Dimitrij

Reputation: 4646

Is it posible in dataweaver 1.0 decapitalise first letter in key name, not value?

I have such input:

{
 Abc: "1",
 BcD: "2",
 ...
 klm: "3",
 ZXC: "4"
} 

I want to get output after transform like this:

{
 abc: "1",
 bcD: "2",
 ...
 klm: "3",
 zXC: "4"
} 

How I can do that? Have been tried like that:

%dw 1.0
%output application/json
---
{
  ($$) replace /^([A-Z])/ with lower $$[1] : $
}

but getting error:

There is no variable named '$$'

Upvotes: 0

Views: 236

Answers (2)

Salim Khan
Salim Khan

Reputation: 4303

Try with this:

Input

{
 "Abc": "1",
 "BcD": "2",
 "klm": "3",
 "ZXC": "4"
} 

Script

%dw 1.0
%input payload application/json
%output application/json
---
payload mapObject {
  (lower ($$)[0] ++ (($$)[1 to -1])):$
}

Output

{
  "abc": "1",
  "bcD": "2",
  "klm": "3",
  "zXC": "4"
}

Upvotes: 2

aled
aled

Reputation: 25837

You have to use mapObject to change the keys. Note that $$ only makes sense in the context of some operators. You need to put the entire key expression between parenthesis. I used pattern matching instead.

Example:

%dw 1.0
%output application/java
%function lowerFirst(s)
    s[0] match {
        'A' -> 'a' ++ s[1..-1],
        'B' -> 'b' ++ s[1..-1],
        // add other letters mapping       
        default -> s
    }
     
---
payload mapObject (lowerFirst($$)): $

Note that you need to complete the letters mapping in the function.

Upvotes: 1

Related Questions