Search code examples
muledataweavemulesoftmule4

How to perform conditional concatenation of strings in Mule 4?


Let's say I have three fields

{
    "Name": "Ben",
    "City": "London",
    "Country": "United Kingdom"
}

Now, when any one of these fields are empty, I don't want it to be concatenated in the final response.

Example: if all three fields are populated, final result: "Ben AND London AND United Kingdom", if there are only two (let's say City and Country): "London AND United Kingdom", if there is only one: "Ben"


Solution

  • You can use either ways shown below:

    %dw 2.0
    output application/json
    ---
    payload pluck ((value, key, index) -> (value)) reduce ((item, accumulator) -> accumulator ++ " AND " ++ item)
    
    %dw 2.0
    output application/json
    ---
    payload pluck ((value, key, index) -> (value)) joinBy  " AND "