Mule4 DataWeave值匹配报错,求正确实现方案
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
.resultcould 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
driveskey 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

