0
votes

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="{ &quot;products&quot;: [{      &quot;product&quot;: &quot;phone&quot;,         &quot;price&quot;: &quot;499.99&quot;   }]}" mimeType="application/json" doc:name="Variable"/>
        <set-payload value="[{  &quot;stores&quot;: {   &quot;storeId&quot;: &quot;1234&quot;  } }]" 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

1
Can you send me the expected JSON response?? I assume the above mentioned "stores JSON Payload" and "Products json" are the input payload's. - Senthil c
Given that Mule stores your Json objects as a string at first, your payload is not currently iterable. If you place a break-point before the for-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öoz
This is because the "payload.stores" will be returned as null, so if there's no value (or at least not one that the for-each can use), then it will never enter the loop. You should change that statement to payload[0].stores to explicitly call the first element in your object. - Möoz
In order to truly help you though, you need to tell us the actual payload you're expecting: Do you want a list of all stores (ID's) and the products within that store? - Möoz

1 Answers

0
votes

Thanks for looking at this. I found a solution.

When I updated my foreach collection to specify json: instead of payload <foreach collection="json:" doc:name="For Each"> then I found it completed successfully and it can iterate through the loop without the need to convert it to a Java list of hashmap.

I'm still a little confused why this works because even though I converted to Java to iterate through the list I converted the payload back to JSON using Object to JSON transformer but looks like it was not truly converted in this instance and the flow still treated it as Java.