Gorodeckij Dimitrij
Gorodeckij Dimitrij

Reputation: 4646

How convert json object to lower case in dataweave 1.0?

Hello I have a json file as payload and strugling to do Transformation to lower case of elements and they values.

{
 "Name" : "John".
 "e-mails" : ['[email protected]','[email protected]']
}

if its no array in values then this one works fine like where

but how to deal with arrays?

expected output:

{
 "name" : "john".
 "e-mails" : ['[email protected]','[email protected]']
}

any advice?

Upvotes: 0

Views: 1973

Answers (1)

aled
aled

Reputation: 25837

You need to use a recursive function to cover for the other types.

%dw 1.0
%output application/json
%function lowerAll(x)
        x match {
          :object ->  $ mapObject {
                (lower $$): lowerAll($) // assumes all keys are strings
          },   
          :array  ->  $ map lowerAll($),
          :string -> lower $,
          default  ->  $
        }  
---
lowerAll(payload)

Input:

{
 "Name" : "John",
 "e-mails" : ["[email protected]","[email protected]"]
}

Output:

{
  "name": "john",
  "e-mails": [
    "[email protected]",
    "[email protected]"
  ]
}

Upvotes: 1

Related Questions