Dataweave 过滤条件不适用于小于等于

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

我有这个有效负载和 dataweave 代码,但在下面的 dataweave 小于工作但不等于?有人可以帮忙吗..如何处理平等?

有效负载:

[{
        "Id": "1",
        "countryName": "Austria"
    },
    {
        "Id": "2",
        "countryName": "Belgium"
    },
    {
        "Id": "3",
        "countryName": "Canada"
    },
    {
        "Id": "4",
        "countryName": "Dutch"
    }]

Datweave:

%dw 2.0
output application/json
---
payload filter ($.countryName >= "A" and $.countryName <= "D")

输出:

[
  {
    "Id": "1",
    "countryName": "Austria"
  },
  {
    "Id": "2",
    "countryName": "Belgium"
  },
  {
    "Id": "3",
    "countryName": "Canada"
  }
]

(https://i.stack.imgur.com/aQMGl.png)

期待:

我期待输出中也出现以“D”开头的国家名称

filter mule dataweave mulesoft mule4
2个回答
0
投票

您可以尝试下面的DataWeave,它可以过滤所有以A到D开头的国家/地区名称

%dw 2.0
output application/json
---
payload filter ((item,index) -> item.countryName matches /^([ABCD]).*/)

此脚本会过滤输入有效负载,以仅包含那些国家名称按预期以“A”、“B”、“C”或“D”开头的对象。正则表达式 ^([ABCD]).* 有效地捕获以任意字母“A”到“D”开头的国家/地区名称。在字符串 (^) 开头之后使用字符类 [ABCD] 可确保只有符合条件的项目才会包含在输出中。

下面是工作代码的屏幕截图:


0
投票

如果您使用 索引选择器更改条件以仅过滤国家/地区的第一个字符,则它会起作用:

%dw 2.0 output application/json --- payload filter ($.countryName[0] >= "A" and $.countryName[0] <= "D")
输出:

[ { "Id": "1", "countryName": "Austria" }, { "Id": "2", "countryName": "Belgium" }, { "Id": "3", "countryName": "Canada" }, { "Id": "4", "countryName": "Dutch" } ]
    
© www.soinside.com 2019 - 2024. All rights reserved.