2

I have a code like this

 {name: 'Data Weave'} mapObject {(upper $$ ) : $}

which dynamically takes map object and upper cases key. below is the output

   {
"NAME": "Data Weave"
   }

When I remove parenthesis that are enclosed to "upper" keyword. data weave is throwing error like this.

I have a syntax confusion exactly where to use parenthesis in the data weave language. in the above code why there is need to use parenthesis. how the compiler interprets the conditions or expression that are enclosed between parenthesis.

kushma gonna
  • 236
  • 3
  • 19

2 Answers2

5
%dw 2.0
output application/json
---
{name: 'Data Weave'} mapObject {(upper($$) ) : $}

I think you just forget parenthesis for the upper fonction. parenthesis surrounding upper fonction are still needing to say to DW the key is not static but dynamically evaluated. To summarize:

  • upper($$) put your $$ in upper case => Parenthesis are here to pass arguments to the function
  • (upper($$)) because the key is the result of an evaluation => Parenthesis are here to evaluate an expression and return the result
2

AFAIK there are three different meanings to parenthesis in DW:

1) Precedence operators eg (1 + 1) * 10

2) Field evaluators, your use case and what @Sebastien Colas is describing.

3) Destructors of Objects and Arrays of objects into pairs of keys and values WHEN they appear on their own inside {}:

%dw 2.0
output application/dw
var d = [
    {
        a: 1,
        b: 2,
        c: 3
    },
    {
        d: 4,
        e: 5
    }
]
---
[
    {
        (d)
    },
    {
        (d[1]),
        f: 6
    }   
]
George
  • 2,758
  • 12
  • 16