01 API Notes
01 API Notes
When transforming XML to JSON you may run into a couple of difficulties:
XML only allows one root element, while JSON allows multiple.
There is no array concept in XML (only repeated elements), however the JSON
data structure is based on arrays and objects.
To overcome these issues, DataWeave provides the following solutions:
To transform XML repeated elements into a JSON array, use the asterisk “*”
DataWeave selector to create it.
As JSON could have multiple root elements, you don’t have to do anything
specific to generate the output.
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
</mule>
<mule xmlns:http=http://www.mulesoft.org/schema/mule/http
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
</mule>
*******************************************
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.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/db
http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
</mule>
*******************************************
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.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/db
http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message >
</ee:message>
</ee:transform>
</flow>
</mule>
*******************************************************
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.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/mule-apikit
http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd ">
<apikit:config name="american-flights-api-config" api="resource::a5bbf80e-03e5-
4f32-9968-0b69770d1024:american-flights-api:1.0.1:raml:zip:american-flights-
api.raml" outboundHeadersMapName="outboundHeaders"
httpStatusVarName="httpStatus" />
<flow name="american-flights-api-main">
<http:listener config-ref="HTTP_Listener_config" path="/api/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:router config-ref="american-flights-api-config" />
<error-handler>
<on-error-propagate type="APIKIT:BAD_REQUEST">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Bad request"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">400</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Method not allowed"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">405</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not acceptable"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">406</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unsupported media type"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">415</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not Implemented"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">501</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="american-flights-api-console">
<http:listener config-ref="HTTP_Listener_config" path="/console/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:console config-ref="american-flights-api-config" />
<error-handler>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="put:\flights\(ID):application\json:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight updated (but not really)"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="delete:\flights\(ID):american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight deleted (but not really)"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="get:\flights:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[
{
ID: 1,
code: "ER38sd",
price: 400,
departureDate: "2017/07/26",
origin: "CLE",
destination: "SFO",
emptySeats: 0,
plane: {
"type": "Boeing 737",
totalSeats: 150
}
},
{
ID: 2,
code: "ER45if",
price: 540.99,
departureDate: "2017/07/27",
origin: "SFO",
destination: "ORD",
emptySeats: 54,
plane: {
"type": "Boeing 777",
totalSeats: 300
}
}
]]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="get:\flights\(ID):american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
ID: 1,
code: "ER38sd",
price: 400,
departureDate: "2017/07/26",
origin: "CLE",
destination: "SFO",
emptySeats: 0,
plane: {
"type": "Boeing 737",
totalSeats: 150
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="post:\flights:application\json:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight added {but not really}"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>
********************************************************************
*********************************************
Mule configuration file XML with no object it it
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
</mule>
*********************************************
Global Configuration Properties XML
<configuration-properties doc:name="Configuration properties"
doc:id="808cb43d-ea15-4bca-9977-b66d6909cafe" file="config.yaml" />
*********************************************
HTTP Listner configuration connection-
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener
config" doc:id="888e991b-a5ac-4d62-858e-79abcff9fb7a" >
<http:listener-connection host="0.0.0.0" port="${http.port}" />
</http:listener-config>
*********************************************
Adding Mule meta data type
After you add first metadata type application-type.xml file will be created.
]]></types:shape>
</types:type>
type flight_json = {|
airline: String,
flightCode: String,
fromAirportCode: String,
toAirportCode: String,
departureDate: String,
emptySeats: Number {"typeId": "int"},
totalSeats: Number {"typeId": "int"},
price: Number {"typeId": "int"},
planeType: String
|} {"example":
"{\\\"airline\\\":\\\"United\\\",\\\"flightCode\\\":\\\"ER38sd\\\",\\\"fromAirportC
ode\\\":\\\"LAX\\\",\\\"toAirportCode\\\":\\\"SFO\\\",\\\"departureDate\\\":\\\"May
21,
2016\\\",\\\"emptySeats\\\":0,\\\"totalSeats\\\":200,\\\"price\\\":199,\\\"planeTyp
e\\\":\\\"Boeing 737\\\"}"}
]]></types:shape>
</types:type>
</types:catalog>
</types:mule>
*********************************************
validation is true xml
<validation:is-true doc:name="Is true" doc:id="c57d5d35-daef-4bc6-b9c2-
a78e2334328a" expression="#[['SFO','LAX','CLE','PDX','PDF'] contains vars.code]"
message="#['Invalid destination ' ++ (vars.code default ' ')]"/>
*********************************************
second transformation with the same component variable name to DWoutput
<ee:transform doc:name="Transform Message" doc:id="409552bf-0e35-421a-
912a-95c4a4cb96d0" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0 output application/java
---
payload
]]></ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="DWoutput" ><![CDATA[%dw 2.0
output application/java
---
payload]]>
</ee:set-variable>
</ee:variables>
</ee:transform>
*****DataWeave script to an external file DataWeave json_flight_playground*****
a new file json_flight_playground.dwl will be created under src/main/resources.
when you post josn message, and if tranform message has output application/java
then payload will have linkedhashmap
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************