user3165854
user3165854

Reputation: 1655

Get field value from JSON array in Mule flowVars

I am using Anypoint 6.1 and Mule 3.8.1 and have a flowVar which holds a JSON object that looks like this:

{
  "stores": [
    [
      "store1"
    ],
    [
      "store2"
    ]
   ]
}

While I loop through the payload I want to pull the value from the flowVars that relates to the object being processed from the payload using the counter value as the position of the array. I don't want to move it to an array because I would then need to add an extra step to format the value for use in a database query e.g. add " which are already there in JSON.

How can I do this?

So far I have got something like this but it is wrong:

#[flowVars.storeData[json:stores[0]]]

Thanks

Upvotes: 0

Views: 2623

Answers (1)

catpaws
catpaws

Reputation: 2283

Here's an example that I think mimics what you're doing. In the payload, I set the json {"stores":[["store1"],["store2"]]}. Before putting it into the variable, I use the transform message (dataweave) to return the indexed store as a string. The payload.stores[1] returns array ["store2"] and joinBy "" removes the array brackets leaving "store2".

     <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
     <flow name="myprojectFlow">
       <http:listener config-ref="HTTP_Listener_Configuration" path="/stores" allowedMethods="GET" doc:name="HTTP"/>
       <set-payload value="{&quot;stores&quot;:[[&quot;store1&quot;],[&quot;store2&quot;]]}" doc:name="Set Payload"/>
       <dw:transform-message metadata:id="adf13229-f050-43dd-bffb-ca2319a3723b" doc:name="Transform Message">
          <dw:input-payload mimeType="application/json"/>
          <dw:input-variable mimeType="application/json" variableName="mystores"/>
          <dw:set-payload><![CDATA[%dw 1.0
          %output application/json
           ---
           payload.stores[1] joinBy ""
          ]]></dw:set-payload>
       </dw:transform-message>
       <set-variable variableName="mystore" value="#[payload]" mimeType="application/json" doc:name="Variable"/>
       <logger message="#[payload]" level="INFO" doc:name="Logger"/>
     </flow>

The logger returns:

INFO 2017-01-17 15:31:24,242 [[myproject].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: "store2"

Upvotes: 0

Related Questions