You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mule4 DataWeave值匹配报错,求正确实现方案

解决Mule4 DataWeave中匹配指定名称的Drive条目问题

Hey Daniel, let's work through why your DataWeave code is erroring out and get it working as expected.

First, let's break down the core issues:

  • Depending on how your input payload is structured when it reaches DataWeave, you might be referencing a non-existent path (the extra .result could be the culprit if your input doesn't have that top-level key).
  • Your original code returns just the filtered array, but you need to wrap that result back into an object with the drives key to match your desired output format.

Let's fix the code

There are two common scenarios based on your actual input payload:

Scenario 1: Your input payload is the result object itself

If the data coming into DataWeave looks like this (no top-level result key):

{"drives": [{"id": "0AC_FdkeL63mHUk9PVA", "name": "QA"}, {"id": "0AC_Ype39GAOpUk9PVA", "name": "Salesforce.com"}, {"id": "0AA9MLlEbuhRfUk9PVA", "name": "UAT-zz-SFJobs-2020-10"}]}

Use this code to filter and wrap the result correctly:

%dw 2.0
output application/json
---
{
    drives: payload.drives filter ((item, index) -> item.name == "Salesforce.com")
}

Scenario 2: Your input payload includes the top-level result key

If your input is exactly the structure you shared (with result as the top-level key):

{"result": {"drives": [{"id": "0AC_FdkeL63mHUk9PVA", "name": "QA"}, {"id": "0AC_Ype39GAOpUk9PVA", "name": "Salesforce.com"}, {"id": "0AA9MLlEbuhRfUk9PVA", "name": "UAT-zz-SFJobs-2020-10"}]}}

Adjust the path to include .result while still wrapping the filtered array:

%dw 2.0
output application/json
---
{
    drives: payload.result.drives filter ((item, index) -> item.name == "Salesforce.com")
}

Expected Output

Either code variant will produce your desired output:

{
    "drives": [{"id": "0AC_Ype39GAOpUk9PVA", "name": "Salesforce.com"}]
}

Bonus Tip

If you want case-insensitive matching (e.g., catch "salesforce.com" or "SALESFORCE.COM"), replace the equality check with the equalsIgnoreCase method:

item.name equalsIgnoreCase "Salesforce.com"

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:37:47