过滤数组中的对象并保留 Mule4 中的其他字段

发布于 2025-01-13 08:32:53 字数 957 浏览 6 评论 0原文

我在请求负载中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2025-01-20 08:32:53

您的输入和预期输出都是不同的。

注意 -> ""(长度为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"
      }
    ]
  }
}

Your Input and Expected Output both are different.

Note -> There is a difference between "" (length is 0) and null (no value allocated)

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 != ""))

Output

{
  "Fruits": {
    "Types": [
      {
        "Field1": "value1",
        "Field2": "value2",
        "Color": {
          "Types": [
            {
              "Order": "test",
              "Cost": "22"
            }
          ]
        }
      }
    ]
  },
  "Color": {
    "Types": [
      {
        "Order": "test",
        "Cost": "22"
      }
    ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文