过滤数组中的对象并保留 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"
}
]
}
}
I have 2 objects in request payload which needs to be filtered based on a field value in Mule4.
Request 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": ""
}
]
}
}
In the above payload, all the fields are required but for Color.Types array, we need to filter only the object where cost != null from both Color objects above.
Expected output json:
{
"Fruits": {
"Types": [
{
"Field1": "value1",
"Field2": "value2",
"Color" : {
"Types": [
{
"Order": "test123",
"Cost": "44"
}
]
}
}
]
},
"Color" : {
"Types": [
{
"Order": "test",
"Cost": "22"
}
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的输入和预期输出都是不同的。
注意 -> ""(长度为0)和null(没有分配值)之间有区别
DW
输出
Your Input and Expected Output both are different.
Note -> There is a difference between "" (length is 0) and null (no value allocated)
DW
Output