Changes in Dataweave 2.0 from Dataweave 1.0

Dataweave version in mule 4.1 is 2.0 which was 1.0 in 3.9 version. With version up-gradation, there come several changes in both header and body sections. Below are a few of the major changes we see –

  1. DataWeave Header Changes:
    Dataweave body contains the script which does convert the input payload into the desired output. Below are the changes we see in this section

    Dataweave header directives have changes in terms of their declaration. Let us look at below two image

    Dataweave 1.0


    Dataweave 2.0

    Below are the changes we see
    • dw version changed from 1.0 to 2.0
    • Rather than the dw version, no other directives start with ‘%’
    • ‘function’ is now ‘fun’ and the separation between function signature and body is ‘=’ which was space in dw 1.0
    • ‘namespace’ is now ‘ns’
    • Also, dataweave language has now separated the functions to separate modules. The way to import is now using ‘import’ 
    • dw 2.0 supports the creation of custom type using ‘type’
  2. DataWeave Body Changes:
    Dataweave body contains the script which does convert the input payload into the desired output. Below are the changes we see in this section
    • Operators are now functions
    • Change in data type names
    • Change in conditional logic
    • Allowing null input
    • Support of a parameter for index
      1. Operators are now functions
        All the operators in dw 1.0 are now functions in dw 2. The argument to the function is surrounded by parentheses.

        Dataweave 1.0


        Dataweave 2.0


        Output:
      2. Change in data type names
        The types in dw 1.0 were all in lowercase and start with ‘:’. In dw 2.0 the syntax does not have ‘:’ and it starts with a capital letter

        Dataweave 1.0


        Dataweave 2.0


        Output:
      3. Change in conditional logic
        dw 1.0 uses when, unless and otherwise to implement conditional logic. However, dw 2.0 supports if, else and elseif

        Dataweave 1:


        Dataweave 2:


        Output:
      4. Allowing null input
        The functions like map, mapObject do not support null in dw 1.0. However, dw 2.0 supports null in functions like map, mapObject, flatten, filter, filterObject, etc.

        Dataweave 1.0
        Below script, in dw 1.0 throws an error at position 6 and the error says that for map operator array is expected and found null


        Dataweave 2.0
        While running the below script in dw 2.0 we do not get any error for the null value being found
      5. Support of a parameter for index:
        While operating on an object dw 2.0 supports index along with value and keys for functions mapObject, pluck, and filterObject. The new lambda expression has three parameters, – value, key, and index. They can be referred as $, $$ and $$$ respectively

        Dataweave 2.0


        Output:

Along with the above changes below are the few more to mention.

  • Dataweave 1.0 supports automatic coercion of object to array. The map function which does run over an array will not throw any error in dw 1.0 in this case. Bu tin dw 2.0 it will. As auto coercion from object to array is not supported on dw 2.0
  • Separate function for object and array. In dw 1.0 we used ‘filter’ for both object and array. But in dw 2.0 they are separated and named as ‘filter’ and ‘filterObject’ to operate on array and object respectively.
  • There are several binary functions introduced like isBlank, isDecimal, isEven, isLeapYear etc.
  • Certain functions in dw 2.0 support binary input along with the similar definition they had in dw 1.0. So below snipped is valid in dw 2.0

    However, line 2 is not valid in dw 1.0
  • Dw 2.0 separates the core function from the module, which is created for a specific group of functions.

This is now the complete list of changes we have from dw 1.0 to dw 2.0. We recommend following mule 4.1 dataweave reference for more on the changes. Also, keep an eye on the release notes of mule runtime versions to see the upgrade in dataweave 2.0

Tags: No tags

Leave A Comment

Your email address will not be published. Required fields are marked *