Search code examples
muleanypoint-studiopayloaddataweave

Dataweave code not working mule 4 which was working in mule3


The following code snippet was working in 3.8 but not working in 4.1:

output application/java
var csv = payload
---
(csv map $ reduce ((val,acc) -> ((acc) ++ ((val)) ))) map ($ replace ',' with '\t')

input payload: 

{"D01":{"AK":"D,01,AK,0,0,0,0,0,-2.89,0.00,0,0,0,0,0",
        "AL":"D,01,AL,829.23,18506.35,0,0.00,0,-6610.91,0.00,0,0,0,159.66,-1.94"},
"D02.1":{"AK":"D,02.1,AK,0,0,0,0,0,-6.76,0.00,0,0,0,0,0",
        "AL":"D,02.1,AL,7733.77,304148.90,0,0.00,0,-42791.15,0.00,0,0,0,1347.09,-8.88"}
} 

enter code here

Expected Output: [ "D\t01\tAK\t0\t0\t0\t0\t0\t-2.89\t0.00\t0\t0\t0\t0\t0", "D\t01\tAL\t829.23\t18506.35\t0\t0.00\t0\t-6610.91\t0.00\t0\t0\t0\t159.66\t-1.94", "D\t02.1\tAK\t0\t0\t0\t0\t0\t-6.76\t0.00\t0\t0\t0\t0\t0", "D\t02.1\tAL\t7733.77\t304148.90\t0\t0.00\t0\t-42791.15\t0.00\t0\t0\t0\t1347.09\t-8.88" ]

Am getting below Error:

org.mule.runtime.core.internal.exception.OnErrorPropagateHandler:


Message : "You called the function 'map' with these arguments:
1: String ("{\"D01\":{\"AK\":\"D,01,AK,0,0,0,0,0,-2.89,0.00,0,0,0,0,0\", \"AL\":\"D,01,A...)
2: Function (($:Any, $$:Any) -> ???)

But it expects arguments of these types:
1: Array
2: Function

13| (csv map $ reduce ((val,acc) -> ((acc) ++ ((val)) ))) map ($ replace ',' with '\t') ^^^^^^^^^


Solution

  • I think the problem there is, map in DataWeave 2 does not work on Object (See changes). DataWeave 1 allowed that and hence your code was valid for DataWeave 1.

    Based on the output with DataWeave 1, I think you can use following code for DataWeave 2 -

    %dw 2.0
    output application/java
    var csv = payload
    ---
    flatten ((csv pluck $) map ($ pluck $)) map ($ replace ',' with '\t')
    

    pluck will split an object into two arrays - values ($) and keys ($$).