如何在 Mule 4 中执行字符串的条件连接?

问题描述 投票:0回答:2

假设我有三个字段

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

现在,当这些字段中的任何一个为空时,我不希望它在最终响应中连接起来。

例子: 如果所有三个字段都被填充,最终结果:

"Ben AND London AND United Kingdom"
, 如果只有两个(比如说城市和国家):
"London AND United Kingdom"
, 如果只有一个:
"Ben"

mule dataweave mulesoft mule4
2个回答
2
投票

首先尝试从 payload 中过滤掉空值,然后使用 joinBy 连接字符串:

%dw 2.0
output application/json
import * from dw::core::Strings
var payload = { "Name": "Ben", "City": "London", "Country": "United Kingdom" }
---
(valuesOf(payload) filter !isBlank($)) joinBy " AND "

0
投票

您可以使用以下任一方式:

%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 "
© www.soinside.com 2019 - 2024. All rights reserved.