I have a Json payload and Json flowVars. I am populating the Products array in the Stores Json payload with a Products Json flowVar but I'm seeing the following error:
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)
The logger shows the payload and flowVar to be in json format but the error looks like it is reading it as a hashmap. Not sure what is causing the error?
Test URL:
http://localhost:8083/api/test
XML flow:
<?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>
Stores JSON Payload:
{
"stores": {
"storeId": "1234"
},
"products": []
}
Products
{
"products": [{
"product": "phone",
"price": "499.99"
}]
}
Expected Result
{
"stores": {
"storeId": "1234"
},
"products": [{
"product": "phone",
"price": "499.99"
}]
}
Thanks
payloadis not currently iterable. If you place a break-point before thefor-each(maybe on the "JSON to Object" transformer) and step through, you will see that your loop never gets fired. You should also see the following message in your console:org.mule.routing.ExpressionSplitter: Splitter returned no results. If this is not expected, please check your split expression. If this is the case, then convert your payload into an iterable first. - Möozpayload.stores" will be returned asnull, so if there's no value (or at least not one that thefor-eachcan use), then it will never enter the loop. You should change that statement topayload[0].storesto explicitly call the first element in your object. - Möoz