ddog0823
ddog0823

Reputation: 17

Dataweave 1.0 to 2.0

Trying to convert the below dataweave from 1.0 to 2.0, but everything I've tried gives one of the following errors. Any ideas please on how to write the same in dataweave 2.0?

Invalid input "map {Entity: $.VENDOR_SITE_CODE[0..2", expected PropertyName or ? Invalid input "filter (($.VENDOR_SITE_CODE[0..3", expected PropertyName or ?

%dw 1.0
%output application/java
%function checkNull(value) '' when value == null otherwise value
---
payload.MMAP_SUPPLIER_DETAILS_OBJ_TYPE.SITES.*MMAP_SUPPLIER_SITE_OBJ_TYPE 
    filter (($.VENDOR_SITE_CODE[0..3] == 'CHN '
             or $.VENDOR_SITE_CODE[0..3] == 'FDL '
             or $.VENDOR_SITE_CODE[0..3] == 'HPP '
             or $.VENDOR_SITE_CODE[0..3] == 'MEX '
             or $.VENDOR_SITE_CODE[0..3] == 'P&A '
             or $.VENDOR_SITE_CODE[0..3] == 'STC ')
             and ($.ORG_ID == '116' or $.ORG_ID == '5195')
            and checkNull($.INACTIVE_DATE) == '')
    map {
        Entity: $.VENDOR_SITE_CODE[0..2]
    }


%dw 2.0
output application/java
fun checkNull(value) = if (value == null) '' else value
---
payload.MMAP_SUPPLIER_DETAILS_OBJ_TYPE.SITES.*MMAP_SUPPLIER_SITE_OBJ_TYPE
    filter (($.VENDOR_SITE_CODE[0..3] == 'CHN '
             or $.VENDOR_SITE_CODE[0..3] == 'FDL '
             or $.VENDOR_SITE_CODE[0..3] == 'HPP '
             or $.VENDOR_SITE_CODE[0..3] == 'MEX '
             or $.VENDOR_SITE_CODE[0..3] == 'P&A '
             or $.VENDOR_SITE_CODE[0..3] == 'STC ')
            and ($.ORG_ID == '116' or $.ORG_ID == '5195')
            and checkNull($.INACTIVE_DATE) == '')
    map {
        Entity: $.VENDOR_SITE_CODE[0..2]
    }

Upvotes: 0

Views: 595

Answers (1)

Michael Jones
Michael Jones

Reputation: 1910

Some things have changed. First, the output isn't prefaced with a %, and if statements are now in the format of if (condition) result else otherResult. Ranges are also now specified using START to FINISH, eg: 0 to -1 selects the entire range.

Try this:

%dw 2.0
output application/java
fun checkNull(value) = if (value == null) '' else value
---
payload.MMAP_SUPPLIER_DETAILS_OBJ_TYPE.SITES.*MMAP_SUPPLIER_SITE_OBJ_TYPE 
    filter (($.VENDOR_SITE_CODE[0 to 3] == 'CHN '
             or $.VENDOR_SITE_CODE[0 to 3] == 'FDL '
             or $.VENDOR_SITE_CODE[0 to 3] == 'HPP '
             or $.VENDOR_SITE_CODE[0 to 3] == 'MEX '
             or $.VENDOR_SITE_CODE[0 to 3] == 'P&A '
             or $.VENDOR_SITE_CODE[0 to 3] == 'STC ')
             and ($.ORG_ID == '116' or $.ORG_ID == '5195')
            and checkNull($.INACTIVE_DATE) == '')
    map {
        Entity: $.VENDOR_SITE_CODE[0 to 2]
    }

Like @aled mentioned though it would be helpful to see the input and expected output.

Upvotes: 2

Related Questions