Mule 3.8.5中DataWeave转换JSON Payload遇数组类型比较错误
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
Incorrect null comparison for arrays
In DataWeave 1.0, directly comparing an array tonull(likepayload.result != null) isn't valid—this triggers a type mismatch because you're trying to compare an array type with a null value.Case mismatch in field names
Your input JSON usesbatchId(capital D), but your DataWeave script referencesbatchid(lowercase d). This would either returnnullfor that field or cause unexpected behavior, and doesn't match your expected output format.Unparsed Payload Type
Looking at your error log, thePayload Typeisjava.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 treatpayload.resultas 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 notnull) 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

