DataWeave has a stanard function to join array elements into a string value: joinBy
in the dw::Core
module. The joinBy
function takes an array as first argument and a string value that is used as separator. The array should have string values or values that can be coerced into string values.
In the following example we use the function joinBy
that will join elements in an array with a given separator:
Source
%dw 2.0 output application/json --- { // Join with default separator ("") using infix notation join: ["a", "b", "c"] joinBy "", // Join with ", " as separator separator: joinBy(["a", "b", "c"], ", "), // Elements can also be non-string values // if they can be coerced into string values range: [1, 2, 3, 4] joinBy "|" }
Output
{ "join": "abc", "separator": "a, b, c", "range": "1|2|3|4" }
Written with DataWeave 2.4.