Search code examples
mulemule-studiodataweavemule-componentanypoint-studio

Construct String from Array in Dataweave 2.0


I am on Mule 4. I need to create a "filter string" (to be used in an API call) from an array. A sample input array is given below:

[
  "123AAA","123BBB","123CCC","123DDD","123EEE"
]

I need to get an output string like this using dataweave 2.0 ID = '123AAA#DT' OR ID = '123BBB#DT' OR ID = '123CCC#DT' OR ID = '123DDD#DT' OR ID = '123EEE#DT'

I tried using joinBy function but since this is in an array, it was giving an error. Please advise.


Solution

  • It seems a good candidate for reduce().

    %dw 2.0
    output application/json
    ---
    payload reduce ((item, accumulator="") -> "ID = '" ++ item ++ "#DT'" ++ (if (accumulator != "")  " OR " ++ accumulator else "" ))