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

Mule 3.8.5中DataWeave转换JSON Payload遇数组类型比较错误

Troubleshooting and Fixing DataWeave 1.0 Error in Mule 3.8.5

Let's break down what's causing the You cannot compare a value of type ::array error and fix it step by step:

Root Causes of the Error

  1. Incorrect null comparison for arrays
    In DataWeave 1.0, directly comparing an array to null (like payload.result != null) isn't valid—this triggers a type mismatch because you're trying to compare an array type with a null value.

  2. Case mismatch in field names
    Your input JSON uses batchId (capital D), but your DataWeave script references batchid (lowercase d). This would either return null for that field or cause unexpected behavior, and doesn't match your expected output format.

  3. Unparsed Payload Type
    Looking at your error log, the Payload Type is java.lang.String, meaning the Transform Message processor is receiving a raw JSON string instead of a parsed object. Your ByteArray to Object converter likely isn't configured correctly, so DataWeave can't treat payload.result as an array.

Step-by-Step Fixes

1. Fix the DataWeave Script

Replace your existing script with this corrected version, which addresses the array check and field case issues:

%dw 1.0
%output application/json
---
batchexecution: 
    { 
        batchId: payload.result[0].batchId, 
        status: payload.result[0].status, 
        success: payload.success 
    } when (!isEmpty(payload.result)) 
    otherwise 
    { 
        batchId: 0, 
        status: "Not queued", 
        success: false 
    }
  • !isEmpty(payload.result): This DataWeave 1.0 function safely checks if the array exists (is not null) and has at least one element, avoiding the type mismatch error from direct null comparison.
  • Corrected batchId: Matches the case of the input JSON field, ensuring you get the correct value and the expected output format.

2. Ensure Payload is Parsed to an Object

If your ByteArray to Object converter isn't working, you can force DataWeave to parse the JSON string directly by confirming the input MIME type in your Transform Message config:

<dw:transform-message doc:name="Transform Message" metadata:id="90448cfd-5884-441a-a989-e32e4877ac24">
    <dw:input-payload mimeType="application/json" doc:sample="sample_data\batchreturnObject.dwl"></dw:input-payload>
    <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
batchexecution: 
    { 
        batchId: payload.result[0].batchId, 
        status: payload.result[0].status, 
        success: payload.success 
    } when (!isEmpty(payload.result)) 
    otherwise 
    { 
        batchId: 0, 
        status: "Not queued", 
        success: false 
    }]]></dw:set-payload>
</dw:transform-message>

Setting mimeType="application/json" tells DataWeave to automatically parse the input string into a JSON object, bypassing any issues with the prior converter.

Expected Output

With these fixes, your input JSON will produce the exact expected output:

{ "batchexecution": { "batchId": 1028, "status": "Queued", "success": true } }

If result is empty or missing, you'll get the fallback output as intended:

{ "batchexecution": { "batchId": 0, "status": "Not queued", "success": false } }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:00:11