[英]Unexpected character error when using multiple Json inputs in Dataweave
我有一個Json有效負載和Json flowVars。 我使用Products Json flowVar填充了Stores Json有效負載中的Products數組,但是看到以下錯誤:
INFO 2017-01-15 23:06:32,559 [[test].test-httpListenerConfig.worker.01] org.mule.api.processor.LoggerMessageProcessor: **** Stores JSON Payload: {"storeId":"1234"}
INFO 2017-01-15 23:06:32,574 [[test].test-httpListenerConfig.worker.01] org.mule.api.processor.LoggerMessageProcessor: *** Products JSON flowvar{"products": [{ "product": "phone", "price": "499.99" }]}
ERROR 2017-01-15 23:06:34,489 [[test].test-httpListenerConfig.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Exception while executing:
{stores={storeId=1234}, products=[]}
^
Unexpected character 's' at index 1 (line 1, position 2), expected '"'.
Payload : {"storeId":"1234"}
Payload Type : java.lang.String
Element : /getFlow/processors/3/3 @ test:testcsv.xml:22 (Transform Message)
Element XML : <dw:transform-message metadata:id="43b88f1f-032a-442f-94f7-09fbc853390b" doc:name="Transform Message">
<dw:input-payload mimeType="application/json"></dw:input-payload>
<dw:input-variable mimeType="application/json" variableName="varTest"></dw:input-variable>
<dw:set-payload>%dw 1.0%input payload application/json%output application/json---{products: flowVars.varTest.products map ((product , indexOfProduct) -> {product: product.product,price: product.price})}</dw:set-payload>
</dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing:
{stores={storeId=1234}, products=[]}
^
Unexpected character 's' at index 1 (line 1, position 2), expected '"'.
at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:12)
at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)
記錄器顯示有效負載和flowVar為json格式,但錯誤似乎是將其讀取為哈希圖。 不知道是什么原因引起的錯誤?
測試網址:
http://localhost:8083/api/test
XML流程:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<flow name="getFlow">
<http:listener config-ref="testdata-httpListenerConfig" path="/test" doc:name="HTTP"/>
<set-variable variableName="varTest" value="{ "products": [{ "product": "phone", "price": "499.99" }]}" mimeType="application/json" doc:name="Variable"/>
<set-payload value="[{ "stores": { "storeId": "1234" } }]" mimeType="application/json" doc:name="Set Payload"/>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
<foreach doc:name="For Each" collection="#[payload]">
<json:object-to-json-transformer mimeType="application/json" doc:name="Object to JSON"/>
<logger message="#['**** Stores JSON Payload: ' + payload]" level="INFO" doc:name="Logger"/>
<logger message="#['*** Products JSON flowvar' + flowVars.varTest]" level="INFO" doc:name="Logger"/>
<dw:transform-message metadata:id="43b88f1f-032a-442f-94f7-09fbc853390b" doc:name="Transform Message">
<dw:input-payload mimeType="application/json" doc:sample="C:\getStores.json"/>
<dw:input-variable mimeType="application/json" variableName="varTest" doc:sample="sample_data\list_json_15.json"/>
<dw:set-payload><![CDATA[%dw 1.0
%input payload application/json
%input in1 application/json
%output application/json
---
{
stores: {
storeId: payload.stores.storeId
},
products: flowVars.varTest.products map ((product , indexOfProduct) -> {
product: product.product,
price: product.price
})
}]]></dw:set-payload>
</dw:transform-message>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
</mule>
存儲JSON有效負載:
{
"stores": {
"storeId": "1234"
},
"products": []
}
制品
{
"products": [{
"product": "phone",
"price": "499.99"
}]
}
預期結果
{
"stores": {
"storeId": "1234"
},
"products": [{
"product": "phone",
"price": "499.99"
}]
}
謝謝
感謝您的關注。 我找到了解決方案。
當我更新我的foreach集合以指定json:而不是有效負載<foreach collection="json:" doc:name="For Each">
我發現它已成功完成,並且可以遍歷循環而無需將其轉換為哈希映射的Java列表。
我仍然有些困惑,為什么這行得通,因為即使我轉換為Java來遍歷列表,我還是使用Object to JSON Transformer將有效負載轉換回JSON,但看起來在這種情況下並沒有真正轉換,仍然可以處理流程它就像Java。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.