1

Mule 3.8.3 Studio 6.4.4

I am receiving a XML payload that is a collection of customer numbers. I need to end up pulling each number out and sending it to a messaging queue.

Sample Incoming Data:

<request func="">
  <data>
    <transactions time='1539262470'>
      <transaction set='customers' notifyid='WMS_NADC_CUSTOMERS' type='update'>
        <customers>
          <customer id="CIT_1113-11" t="1539257721" y="U" w="WebUser"></customer>
          <customer id="C42998-2" t="1539261561" y="N" w="WebUser"></customer>
          <customer id="C42998" t="1539262040" y="U" w="WebUser"> </customer>
        </customers>
      </transaction>
    </transactions>
  </data>
</request>

After receiving this I use weave to transform into json in an attempt to more easily access the id's.

%dw 1.0
%output application/json
---
{
    customers: payload.request.data.transactions.transaction.customers.*customer map (cust, indexOfCustomer) ->{
        customer: cust.@id as :string
    }
}

The transformed payload now looks like

{
  "customers": [
    {
      "customer": "CIT_1113-11"
    },
    {
      "customer": "C42998-2"
    },
    {
      "customer": "C42998"
    }
  ]
}

At this point, I am trying to loop through the payload. Setting the for each to payload.get('customers') takes me into a jackson.node.ArrayNode.

I haven't been able to figure out how to access each individual object inside the list. Can one of you please tell me how to do that?

I want to end up placing a payload onto a message queue that looks like

{
  "customer": "C42998"
}
jerney
  • 2,187
  • 1
  • 19
  • 31

1 Answers1

1

The simplest way would be to use a combination of DataWeave to get a Java Iteratable, and a for-each scope. Check out this example (assuming the incoming payload is your XML).

<dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java

%var customers = payload.request.data.transactions.transaction.customers.*customer
---
customers map (customer) ->{
  customer: customer.@id as :string
}]]></dw:set-payload>
</dw:transform-message>
<foreach doc:name="For Each">
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</foreach>

In Mule 3, the for-each won't accept JSON or XML, even if they obviously represent a structure that could be iterated over (like a JSON array, for example). This is why we need %output application/java in the DataWeave transformer. Later, within the for-each scope, we can transform this back to JSON. In your case, just replace the logger with your queue connector to send the messages where needed.

jerney
  • 2,187
  • 1
  • 19
  • 31