"Merge two payload into one in transform component "

880 Views Asked by At

I am trying to add two json payload into one but getting error. I am using scatter gather where I am getting multiple payload and combining the payload into transform message. Error is: Exception while executing: payload[0] map (bookListing) -> using (id = bookListing.bookId) { Type mismatch for 'map' operator found :string, :function required :array, :function.

code:

      <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="file-connector-config" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" />
<flow name="muleRequester">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/requester" doc:name="HTTP"/>
    <scatter-gather doc:name="Scatter-Gather">
        <processor-chain>
            <mulerequester:request resource="file://src/main/resources/input1" returnClass="java.lang.String" doc:name="Retrieve File1"/>
            <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        </processor-chain>
        <processor-chain>
            <mulerequester:request resource="file://src/main/resources/input2" returnClass="java.lang.String" doc:name="Retrieve File2"/>
            <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        </processor-chain>
    </scatter-gather>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
         %output application/json
         ---
         payload[0]  map (bookListing) -> using (id = bookListing.bookId) {
bookId: id,
title:  bookListing.title,
price:  bookListing.price,
(payload[1] filter ($.*bookId contains id)  map (bookAuthor) -> {
    author:bookAuthor.author
        })
    }]]></dw:set-payload>
     </dw:transform-message>
     <file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].json" path="src/main/resources/output"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

payload 1 - [ { "bookId":"101", "title":"world history", "price":"19.99" }, { "bookId":"202", "title":"the great outdoors", "price":"15.99" } ]

payload 2- [ { "bookId":"101", "author":"john doe" }, { "bookId":"202", "author":"jane doe" } ]

Expected output-

[ { "bookId": "101", "title": "world history", "year": "2017", "isbn": "11111", "price": "19.99", "author": "john doe" }, { "bookId": "202", "title": "the great outdoors", "year": "2016", "isbn": "22222", "price": "15.99", "author": "jane doe" } ]

1

There are 1 best solutions below

0
Ryan Carter On BEST ANSWER

The values returned from the files are just string so need to read them into something that can be used by dataweave first. Use the read function like so:

read(payload[0], 'application/json')

and

read(payload[1], 'application/json')

Here is a full example. (I have stubbed out the sample json in the scatter-gather):

    <scatter-gather doc:name="Scatter-Gather">
       <set-payload value="[ { &quot;bookId&quot;:&quot;101&quot;, &quot;title&quot;:&quot;world history&quot;, &quot;price&quot;:&quot;19.99&quot; }, { &quot;bookId&quot;:&quot;202&quot;, &quot;title&quot;:&quot;the great outdoors&quot;, &quot;price&quot;:&quot;15.99&quot; } ]" doc:name="payload1"/>
       <set-payload value="[ { &quot;bookId&quot;:&quot;101&quot;, &quot;author&quot;:&quot;john doe&quot; }, { &quot;bookId&quot;:&quot;202&quot;, &quot;author&quot;:&quot;jane doe&quot; } ]" doc:name="payload2"/>
    </scatter-gather>

    <dw:transform-message doc:name="Transform Message">
       <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
read(payload[0], 'application/json')  map (bookListing) -> using (id = bookListing.bookId) {
bookId: id,
title:  bookListing.title,
price:  bookListing.price,
(read(payload[1], 'application/json') filter ($.*bookId contains id)  map (bookAuthor) -> {
    author:bookAuthor.author
})
}]]></dw:set-payload>
   </dw:transform-message>

Or you could read the whole thing into Json before hand