Sunday, September 4, 2016


Mule ESB: DataWeave Multi/Different Input


This Example explains about handling Multiple Input (JSON,XML) data using DataWeave component.

Pre-requisites

  1. Anypoint Studio 5.2.0
  2. ESB Runtime 3.7.1
  3. JSON Input Data

 Mule Flow 



http configuration 






 flowVariable_JSON 


flowVariable_XML 


DataWeave Logic



[
{
"item": {
"type": "book",
"price": 30,
"properties": {
"title": "Everyday Italian",
"author": [
"Giada De Laurentiis"
],
"year": 2005
}
}
},
{
"item": {
"type": "book",
"price": 29.99,
"properties": {
"title": "Harry Potter",
"author": [
"J K. Rowling"
],
"year": 2005
}
}
},
{
"item": {
"type": "book",
"price": 49.99,
"properties": {
"title": "XQuery Kick Start",
"author": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Kurt Cagle",
"Vaidyanathan Nagarajan"
],
"year": 2003
}
}
},
{
"item": {
"type": "book",
"price": 39.95,
"properties": {
"title": "Learning XML",
"author": [
"Erik T. Ray"
],
"year": 2003
}
}
}
]

Flow Response

<?xml version='1.0' encoding='UTF-8'?>
<books>
<book year="2005">
<price currency="EUR">27.60</price>
<price currency="ARS">262.80</price>
<price currency="GBP">19.80</price>
<title>Everyday Italian</title>
<authors>
<author>Giada De Laurentiis</author>
</authors>
</book>
<book year="2005">
<price currency="EUR">27.5908</price>
<price currency="ARS">262.7124</price>
<price currency="GBP">19.7934</price>
<title>Harry Potter</title>
<authors>
<author>J K. Rowling</author>
</authors>
</book>
</books>

Mule Flow Source

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="File" autoDelete="false" streaming="true" validateConnections="true" pollingFrequency="90000"doc:name="File"/>
<flow name="dataweaveFlow-multi-input">
<http:listener config-ref="HTTP_Listener_Configuration" path="/multi" doc:name="HTTP"/>
<set-variable variableName="two" value="{&quot;USD&quot;:[{&quot;currency&quot;: &quot;EUR&quot;, &quot;ratio&quot;:0.92},{&quot;currency&quot;: &quot;ARS&quot;, &quot;ratio&quot;:8.76},{&quot;currency&quot;: &quot;GBP&quot;, &quot;ratio&quot;:0.66}]}" doc:name="flowVariable_JSON" mimeType="application/json"/>
<set-variable variableName="three" value="&lt;publishedAfter&gt;2004&lt;/publishedAfter&gt;
"doc:name="flowVariable_XML" mimeType="application/xml"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%input payload application/json
%var in1=flowVars['two']
%var in2=flowVars['three']
%output application/xml
---
books: {
(payload filter $.item.properties.year > in2.publishedAfter map using (it = $.item, props = $.item.properties) {
book @(year: props.year): {
(in1.USD map {
price @(currency: $.currency): $.ratio * it.price
}),
title: props.title,
authors: { (props.author map {
author: $
}) }
}
})
}]]></dw:set-payload>
</dw:transform-message>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>