Search code examples
mulemule-studiodataweavemulesoftmule-esb

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" : ['Email1@mail.com','email2@Gmail.com']
}

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" : ['email1@mail.com','email2@gmail.com']
}

any advice?


Solution

  • 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" : ["E1mail1@mail.com","email2@Gmail.com"]
    }
    

    Output:

    {
      "name": "john",
      "e-mails": [
        "e1mail1@mail.com",
        "email2@gmail.com"
      ]
    }