Search code examples
dataweavepayloadmule4

How to exclude a column from payload in mulesoft ftp create step ? (payload is an array of object)


here is the FTP create code :

%dw 2.0
output application/csv separator='|',
header = false
---
payload distinctBy $.integrationid orderBy $.integrationidl
- "integrationid" - "integrationidl"

In the above code i want to use the integrationid and integrationdl columns for order and sort. but i dont want these columns from payload to be printed in the CSV file. i tried the above code as per mulesoft documentation but it does not work. most probably because it is an array. Can you please help how this can be achieved ?


Solution

  • Return from the orderBy() function is an array. The - operator applied to an object and a string removes a key-pair from an object by key name. You can't apply it to the array. It means you need to map each element of the array and remove the keys from each one.

    %dw 2.0
    output application/csv separator='|', header=false
    ---
    payload 
        distinctBy $.integrationid
        orderBy $.integrationidl
        map $ - "integrationid" - "integrationidl"
    

    Another alternative instead of the - operator is to use filterObject() with a condition that filters which key-values remain in each object. The advantage is that we can use contains() with a list of column names in the condition to make the script more generic.

    %dw 2.0
    output application/csv separator='|', header=false
    var filterColumns=["integrationid", "integrationidl"]
    ---
    payload 
        distinctBy $.integrationid
        orderBy $.integrationidl
        map ($ filterObject !(filterColumns contains ($$ as String)))