Search code examples
dataweavemule-esb

What is the purpose of {} in dataweave


I am trying to learn Mule DataWeave 2.0 and not many resources out there beside the mule documentation. any good tutorial you can suggest? With that said, I have the following question
what is the purpose of {} after ---
for example

%dw 2.0
fun toUpper(aString) = upper(aString)
---
toUpper("hello")

returns "HELLO"
But if I put

---
{ toUpper("hello") } 

I get an error


Solution

  • In contrast to most programming languages, {} in DataWeave is not used for scoping of code.

    Here it's used to mark the beginning/ending of an Object (a key/value pair sequence), like in JSON.

    So, your code emits an error because it expects a key before the "hello" string value.

    For example:

    { "some_key": toUpper("hello") }