JSON object 忽略 null

JSON object Ignore null

我对 MuleSoft 和 DataWeave 有点陌生,我正在尝试创建一个 JSON 对象,其中只有来自另一个 JSON 对象的非空值。

假设这是我的 JSON 数组:

{
    str1 : "String 1",
    str2 : "String 2",
    str3 : null,
    str4 : "String 4",
}

我想复制那个 JSON 数组但没有 str3,所以结果应该是这样的:

{
    str1 : "String 1",
    str2 : "String 2",
    str4 : "String 4",
}

谁能帮我解决这个问题?或者至少引导我找到解决方案?

此致

有两种可能的方法:

使用编写器 属性 skipNullOn 如前所述 here

output application/json skipNullOn="everywhere"
---
payload

以编程方式使用 if 条件(这是针对逐个字段的映射)

var b = null
---
{
    a: 1,
    (b: b) if b != null,
    c: 3
}

对于一个对象,您可以转换所有不为空的属性:

%dw 2.0
output application/json
---
payload mapObject (($$): $ ) if (!($ == null))

还有另一种方法:

%dw 2.0
output application/json
var o = {
    str1 : "String 1",
    str2 : "String 2",
    str3 : null,
    str4 : "String 4",
}
---
o filterObject $ != null

这是过滤器对象documentation

选一个你喜欢的:)