过滤数组中的对象并保留Mule4中的其他字段
Filter object in an array and retain other fields in Mule4
我在请求负载中有 2 个对象需要根据 Mule4 中的字段值进行过滤。
请求 json:
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
},
{
"Order": "test1",
"Cost": ""
}
]
}
}
]
},
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
},
{
"Order": "test1",
"Cost": ""
}
]
}
}
在上面的有效负载中,所有字段都是必需的,但对于 Color.Types 数组,我们只需要从上面的两个 Color 对象中过滤 cost != null 的对象。
预期输出json:
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color" : {
"Types": [
{
"Order": "test123",
"Cost": "44"
}
]
}
}
]
},
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
您的输入和预期输出不同。
注意 -> ""(长度为0)和null(没有分配值)有区别
DW
%dw 2.0
output application/json
import * from dw::util::Values
---
payload update ["Color","Types"] with ($ filter ($.Cost != null and $.Cost != "")) update ["Fruits","Types","Color","Types"] with ($ filter ($.Cost != null and $.Cost != ""))
输出
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color": {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
]
},
"Color": {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
我在请求负载中有 2 个对象需要根据 Mule4 中的字段值进行过滤。 请求 json:
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
},
{
"Order": "test1",
"Cost": ""
}
]
}
}
]
},
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
},
{
"Order": "test1",
"Cost": ""
}
]
}
}
在上面的有效负载中,所有字段都是必需的,但对于 Color.Types 数组,我们只需要从上面的两个 Color 对象中过滤 cost != null 的对象。
预期输出json:
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color" : {
"Types": [
{
"Order": "test123",
"Cost": "44"
}
]
}
}
]
},
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
您的输入和预期输出不同。
注意 -> ""(长度为0)和null(没有分配值)有区别
DW
%dw 2.0
output application/json
import * from dw::util::Values
---
payload update ["Color","Types"] with ($ filter ($.Cost != null and $.Cost != "")) update ["Fruits","Types","Color","Types"] with ($ filter ($.Cost != null and $.Cost != ""))
输出
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color": {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
]
},
"Color": {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}