Aditya S
Aditya S

Reputation: 82

Mulesoft Dataweave Converting the key of a mapObject to lower case

Assume the following json with KEY1 and KEY2 in caps. KEY1 and KEY2 needs to be converted to lower case

 {
    "KEY1": {
        "subkey1": "subval1",
        "subkey2": "subval2"
    },
    "KEY2": {
        "subkey1": "subval1",
        "subkey2": "subval2"
    }
  }

this needs to be converted to the following json using data weave.

 {
    "key1": {
        "subkey1": "subval1",
        "subkey2": "subval2"
    },
    "key2": {
        "subkey1": "subval1",
        "subkey2": "subval2"
    }
  }

I tried the following DW syntax, but it did not work

result : payload mapObject (
  lower '$$':$
)

Upvotes: 1

Views: 3681

Answers (3)

Gorodeckij Dimitrij
Gorodeckij Dimitrij

Reputation: 4646

In addition if need to lower case also a values then its will be like:

%dw 1.0
%output application/json
---
{
    result : payload mapObject (
        (lower $$) : (lower $)
    )
}

Upvotes: 0

Ramana Reddy P
Ramana Reddy P

Reputation: 1

You can use the below method to resolve the issue.

%dw 2.0
output application/json
---
{ result: payload mapObject (
    (lower ('$$')): $
)
}

Upvotes: 0

j13
j13

Reputation: 51

The DW you tried should work if you wrap the expression in parenthesis. This ensures that the lower operator is applied to each of the keys first and then that value is used in the map. So for your example:

%dw 1.0
%output application/json
---
{
    result : payload mapObject (
        (lower '$$') : $
    )
}

Interestingly enough, I get an error (mismatched input ':' expecting ')') in my Transform Message using this DW syntax but I am able to run the project without complaints from Anypoint Studio and the DW runs fine. It also works in MEL with the following:

#[dw("{result : payload mapObject ( (lower  '$$' ) : $)}", 'application/json')]

Hope that helps!

Upvotes: 2

Related Questions