Mule 3.8.5 中涉及 JSON 有效负载的 Dataweave 转换错误

Error in Dataweave transformation involving a JSON Payload in Mule 3.8.5

我在 JSON 负载上使用数据编织转换时出错。 JSON 有效载荷是

{
    "requestId": "13431#1638a2abfb8",
    "result": [
        {
            "batchId": 1028,
            "importId": "1028",
            "status": "Queued"
        }
    ],
    "success": true
}

以上有效负载由 RESTful 服务返回,在应用以下数据编织转换之前,我已使用 byteArray 到对象转换器将其转换为对象

%dw 1.0
%output application/json
---
batchexecution:
 {
    batchid:payload.result[0].batchid,
    status: payload.result[0].status,
    success:payload.success 
 }  when ((payload.result != null) and (sizeOf payload.result  > 0)) 
 otherwise
 {
    batchid: 0,
    status:"Not queued",
    success:false
 }

我希望结果对象只有一条记录,并且我检查数组是否为空或其大小是否大于 0。执行转换代码时出现以下错误。不确定这里有什么问题。

我期待以下转换输出,但在执行转换代码时出现错误

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

但我收到以下错误,因为 您无法比较 ::array 类型的值。

Message               : Exception while executing: 
{"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
                                         ^
You cannot compare a value of type ::array.
Payload               : {"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
Payload Type          : java.lang.String
Element               : /marketing-dbmkt-etl-marketoFlow/processors/8 @ marketing-dbmkt-etl-marketo:marketing-dbmkt-etl-marketo.xml:69 (Transform Message)
Element XML           : <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>%dw 1.0%output application/json---batchexecution:{batchid:payload.result[0].batchid,status: payload.result[0].status,success:payload.success}  when ((payload.result != null) and (sizeOf payload.result  > 0))otherwise{batchid: 0,status:"Not queued",success:false}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing: 
{"requestId":"64b3#1638e55058c","result":[{"batchId":1037,"importId":"1037","status":"Queued"}],"success":true}
                                         ^
You cannot compare a value of type ::array.
    at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:10)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.interceptor.AbstractEnvelopeInterceptor.processBlocking(AbstractEnvelopeInterceptor.java:58)
    at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
    at org.mule.processor.AsyncInterceptingMessageProcessor.processNextTimed(AsyncInterceptingMessageProcessor.java:129)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.process(AsyncInterceptingMessageProcessor.java:213)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.process(AsyncInterceptingMessageProcessor.java:206)
    at org.mule.execution.ExecuteCallbackInterceptor.execute(ExecuteCallbackInterceptor.java:16)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:35)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:22)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:30)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:14)
    at org.mule.execution.BeginAndResolveTransactionInterceptor.execute(BeginAndResolveTransactionInterceptor.java:67)
    at org.mule.execution.ResolvePreviousTransactionInterceptor.execute(ResolvePreviousTransactionInterceptor.java:44)
    at org.mule.execution.SuspendXaTransactionInterceptor.execute(SuspendXaTransactionInterceptor.java:50)
    at org.mule.execution.ValidateTransactionalStateInterceptor.execute(ValidateTransactionalStateInterceptor.java:40)
    at org.mule.execution.IsolateCurrentTransactionInterceptor.execute(IsolateCurrentTransactionInterceptor.java:41)
    at org.mule.execution.ExternalTransactionInterceptor.execute(ExternalTransactionInterceptor.java:48)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:28)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:13)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:110)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:30)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.doRun(AsyncInterceptingMessageProcessor.java:205)
    at org.mule.work.AbstractMuleEventWork.run(AbstractMuleEventWork.java:53)
    at org.mule.work.WorkerContext.run(WorkerContext.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

********************************************************************************

这里的问题并不明显,但我以前遇到过同样的问题 - 它与 sizeOf 函数有关,而且 Mule 对其中一些函数应用优先级的方式很差运营商。当你说 (sizeOf payload.result > 0) 时,它首先尝试解析 payload.result > 0 表达式 - 因此你看到的错误(它试图将数组与 0 进行比较)。

请改用 ((sizeOf payload.result) > 0)(出于这个原因,我总是注意将 sizeOf 括在括号中)。

作为旁注,您有 batchid:payload.result[0].batchid - 它应该是 batchId:payload.result[0].batchIdbatchId 中的大写)

每当您在 dataweave 中使用 sizeOf 之类的任何函数时,请尝试使用圆括号将其封装以避免此类错误。

@ghoshyTech 你的情况

   when ((payload.result != null) and ((sizeOf payload.result)  > 0))