简体   繁体   English

Dataweave转换中涉及Mule 3.8.5中的JSON Payload的错误

[英]Error in Dataweave transformation involving a JSON Payload in Mule 3.8.5

I am getting an error while using a dataweave transformation on a JSON Payload. 在JSON有效负载上使用dataweave转换时出现错误。 The JSON Payload is JSON有效负载为

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

The above payload is returned by a RESTful service and I have converted that to a object using byteArray to Object transformer before applying the following dataweave transformation 上面的有效负载是由RESTful服务返回的,在应用以下dataweave转换之前,我已使用byteArray到Object转换器将其转换为对象

%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
 }

I am expecting only one record for the result object and I have a check to see whether the array is null or its size is >0. 我期望结果对象只有一条记录,并且要检查数组是否为null或它的大小是否大于0。 I get the following error when I execute the transformation code. 执行转换代码时出现以下错误。 Not sure what is wrong here. 不知道这里出了什么问题。

I am expecting the following output for the transformation but I am getting the error while executing the transformation code 我期待转换的以下输出,但是在执行转换代码时出现错误

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

But I am getting the following error as You cannot compare a value of type ::array. 但由于您无法比较类型:: 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$1.process(AsyncInterceptingMessageProcessor.java:213)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.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)

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

The problem here is not obvious, but I have come across the same issue before - it's related to the sizeOf function and the poor way that Mule applies precedence to some of it's operators. 这里的问题并不明显,但我之前也遇到过同样的问题-它与sizeOf函数以及Mule对其某些运算符施加优先级的不良方式有关。 When you say (sizeOf payload.result > 0) it's first trying to attempt to resolve the payload.result > 0 expression - hence the error you're seeing (it's trying to compare an array to 0). 当您说(sizeOf payload.result > 0)它首先尝试尝试解析payload.result > 0表达式-因此,您将看到错误(它正在尝试将数组与0进行比较)。

Please use ((sizeOf payload.result) > 0) instead (I always make a point of wrapping sizeOf in parentheses for this reason). 请改用((sizeOf payload.result) > 0) (出于这个原因,我总是在括号内加一个包裹sizeOf的点)。

As a side note, you have batchid:payload.result[0].batchid - it should be batchId:payload.result[0].batchId (capitalisation in batchId ) 附带说明一下,您具有batchid:payload.result[0].batchid它应该是batchId:payload.result[0].batchId (在batchIdbatchId

whenever you are using any function like sizeOf in dataweave try to encapsulate it with round braces to avoid these kinds of errors. 每当您在dataweave中使用诸如sizeOf类的任何函数时,请尝试使用大括号将其封装,以避免此类错误。

@ghoshyTech in your case @ghoshyTech您的情况

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM