Transforming Flight Data with DataWeave
Use DataWeave to transform and match flight data to your API spec.
Add the Transform Message Component
Add a Transform Message component to the flow so you can configure it to return the database payload as JSON in the next section.
-
In Anypoint Code Builder, open
american-ws-anypoint-code-builder.xml. -
Add the Transform Message component below the Database Select operation. Use one of these methods:
-
From the canvas: Click the
(Add component) icon after the Database Select operation. In the menu, open Core Processors (or search for transform), then select Transform Message. -
From the configuration XML: Place your cursor inside the flow, after the closing tag of the Database Select element. Use auto-complete (Ctrl+Space or Cmd+Space), type
ee:transformortransform, and select Transform Message from the list.The IDE inserts the component. If the inserted XML does not include
<ee:message>and<ee:set-payload>with an empty<![CDATA[]]>block, add that structure inside<ee:transform/>so you can add the DataWeave script in the next section.
Example Transform Message component XML:<ee:transform doc:name="" doc:id="51fed3-afee8c"> <ee:message> <ee:set-payload> <![CDATA[]]> </ee:set-payload> </ee:message> </ee:transform>
-
Return the Payload as JSON
-
Open
american-ws-anypoint-code-builder.xmlif it is not already open (you added the Transform Message component in Add the Transform Message Component). -
Ensure the Transform Message component is present below Database Select (you added it in the previous section). In the configuration XML, add the
doc:namevalue Transform Message or similar to the<ee:transform/>component:<ee:transform doc:name="Transform Message" doc:id="423214-fac9c4"> ...
-
Within
<![CDATA[]]>add a DataWeave script that outputs the message payload to a JSON format:<ee:transform doc:name="Transform Message" doc:id="51fed3-afee8c"> <ee:message> <ee:set-payload> <![CDATA[ %dw 2.0 output application/json --- payload ]]> </ee:set-payload> </ee:message> </ee:transform> -
Deploy your app within the IDE by selecting Run > Start Debugging (F5).
For guidance, see Run Your App in Debug Mode.
-
After the app deploys successfully, use a REST Client or browser to trigger a response.
For guidance, see Test Your App.
-
View the response data from the MySQL database, for example:
[ { "planeType": "Boeing 787", "code2": "0001", "takeOffDate": "2016-01-20T00:00:00", "code1": "rree", "fromAirport": "MUA", "price": 541, "seatsAvailable": 0, "toAirport": "LAX", "ID": 1, "airlineName": "American Airlines", "totalSeats": 200 }, ... ]Click for the complete response.
[ { "planeType": "Boeing 787", "code2": "0001", "takeOffDate": "2016-01-20T00:00:00", "code1": "rree", "fromAirport": "MUA", "price": 541, "seatsAvailable": 0, "toAirport": "LAX", "ID": 1, "airlineName": "American Airlines", "totalSeats": 200 }, { "planeType": "Boeing 747", "code2": "0123", "takeOffDate": "2016-01-25T00:00:00", "code1": "eefd", "fromAirport": "MUA", "price": 300, "seatsAvailable": 7, "toAirport": "CLE", "ID": 2, "airlineName": "American Airlines", "totalSeats": 345 }, { "planeType": "Boeing 777", "code2": "0192", "takeOffDate": "2016-01-20T00:00:00", "code1": "ffee", "fromAirport": "MUA", "price": 300, "seatsAvailable": 0, "toAirport": "LAX", "ID": 3, "airlineName": "American Airlines", "totalSeats": 300 }, { "planeType": "Boeing 737", "code2": "1000", "takeOffDate": "2016-01-20T00:00:00", "code1": "rree", "fromAirport": "MUA", "price": 200, "seatsAvailable": 5, "toAirport": "CLE", "ID": 4, "airlineName": "American Airlines", "totalSeats": 150 }, { "planeType": "Boeing 737", "code2": "1093", "takeOffDate": "2016-02-11T00:00:00", "code1": "rree", "fromAirport": "MUA", "price": 142, "seatsAvailable": 1, "toAirport": "SFO", "ID": 5, "airlineName": "American Airlines", "totalSeats": 150 }, { "planeType": "Boeing 787", "code2": "1112", "takeOffDate": "2016-01-20T00:00:00", "code1": "ffee", "fromAirport": "MUA", "price": 954, "seatsAvailable": 100, "toAirport": "CLE", "ID": 6, "airlineName": "American Airlines", "totalSeats": 200 }, { "planeType": "Boeing 777", "code2": "1994", "takeOffDate": "2016-01-01T00:00:00", "code1": "eefd", "fromAirport": "MUA", "price": 676, "seatsAvailable": 0, "toAirport": "SFO", "ID": 7, "airlineName": "American Airlines", "totalSeats": 300 }, { "planeType": "Boeing 737", "code2": "2000", "takeOffDate": "2016-02-20T00:00:00", "code1": "ffee", "fromAirport": "MUA", "price": 300, "seatsAvailable": 30, "toAirport": "SFO", "ID": 8, "airlineName": "American Airlines", "totalSeats": 150 }, { "planeType": "Boeing 737", "code2": "3000", "takeOffDate": "2016-02-01T00:00:00", "code1": "eefd", "fromAirport": "MUA", "price": 900, "seatsAvailable": 0, "toAirport": "SFO", "ID": 9, "airlineName": "American Airlines", "totalSeats": 150 }, { "planeType": "Boeing 777", "code2": "4511", "takeOffDate": "2016-01-15T00:00:00", "code1": "eefd", "fromAirport": "MUA", "price": 900, "seatsAvailable": 100, "toAirport": "LAX", "ID": 10, "airlineName": "American Airlines", "totalSeats": 300 }, { "planeType": "Boeing 737", "code2": "4567", "takeOffDate": "2016-01-20T00:00:00", "code1": "rree", "fromAirport": "MUA", "price": 456, "seatsAvailable": 100, "toAirport": "SFO", "ID": 11, "airlineName": "American Airlines", "totalSeats": 150 } ]If the MySQL test server is not available, this error occurs:
Cannot get connection for URL jdbc:mysql://mudb.learn.mulesoft.com:3306/ : Communications link failure.
To address the issue, try again until you get a successful response.
-
Proceed to Transform the Response Data.
Transform the Response Data
Transform the JSON output to the structure required by your API specification.
Your American Flights API in Anypoint Exchange uses a different structure for its flight data than your request to the database returns.
To find the required JSON structure, search for the JSON example under the GET method for your /flights endpoint. For guidance, see Locate Your API in Exchange or open the public version of the API on Exchange.
| API Specification Response Structure | Actual Response Structure |
|---|---|
|
|
Use DataWeave to transform the response:
-
In the configuration XML, replace the script within the
<![CDATA]]>with a DataWeave mapping:<ee:transform doc:name="Transform Message" doc:id="51fed3-afee8c"> <ee:message> <ee:set-payload> <![CDATA[ %dw 2.0 output application/json --- payload map ( payload01 , indexOfPayload01 ) -> { ID: payload01.ID, code: (payload01.code1 default "") ++ (payload01.code2 default ""), price: payload01.price default 0, departureDate: payload01.takeOffDate as String default "", origin: payload01.fromAirport default "", destination: payload01.toAirport default "", emptySeats: payload01.seatsAvailable default 0, plane: { "type": payload01.planeType default "", totalSeats: payload01.totalSeats default 0 } } ]]> </ee:set-payload> </ee:message> </ee:transform> -
Review your configuration XML:
<http:listener-config name="inbound-request" doc:name="HTTP Config"> <http:listener-connection host="0.0.0.0" port="8081" /> </http:listener-config> <db:config name="Database_Config" doc:name="mySQL DB"> <db:my-sql-connection host="mudb.learn.mulesoft.com" port="3306" user="mule" password="mule" database="training" /> </db:config> <flow name="getFlights"> <http:listener path="flights" config-ref="inbound-request" doc:name="HTTP /flights" /> <db:select doc:name="Query Flights" config-ref="Database_Config" > <db:sql> <![CDATA[Select * FROM american]]> </db:sql> </db:select> <ee:transform doc:name="Transform Message"> <ee:message > <ee:set-payload > <![CDATA[ %dw 2.0 output application/json --- payload map ( payload01 , indexOfPayload01 ) -> { ID: payload01.ID, code: (payload01.code1 default "") ++ (payload01.code2 default ""), price: payload01.price default 0, departureDate: payload01.takeOffDate as String default "", origin: payload01.fromAirport default "", destination: payload01.toAirport default "", emptySeats: payload01.seatsAvailable default 0, plane: { "type": payload01.planeType default "", totalSeats: payload01.totalSeats default 0 } } ]]> </ee:set-payload> </ee:message> </ee:transform> </flow> -
Proceed to Run and Test Your App.
Run and Test Your App
-
Deploy your app within the IDE by executing to Run > Start Debugging (F5).
For guidance, see Run Your App in Debug Mode.
-
After the app deploys successfully, use a REST Client or browser to trigger flow.
For guidance, see Test Your App.
Notice that the transformed data structure conforms to the API requirements, for example:
[ { "ID": 1, "code": "rree0001", "price": 541, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "LAX", "emptySeats": 0, "plane": { "type": "Boeing 787", "totalSeats": 200 } }, ...Click for the complete response.
[ { "ID": 1, "code": "rree0001", "price": 541, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "LAX", "emptySeats": 0, "plane": { "type": "Boeing 787", "totalSeats": 200 } }, { "ID": 2, "code": "eefd0123", "price": 300, "departureDate": "2016-01-25T00:00:00", "origin": "MUA", "destination": "CLE", "emptySeats": 7, "plane": { "type": "Boeing 747", "totalSeats": 345 } }, { "ID": 3, "code": "ffee0192", "price": 300, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "LAX", "emptySeats": 0, "plane": { "type": "Boeing 777", "totalSeats": 300 } }, { "ID": 4, "code": "rree1000", "price": 200, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "CLE", "emptySeats": 5, "plane": { "type": "Boeing 737", "totalSeats": 150 } }, { "ID": 5, "code": "rree1093", "price": 142, "departureDate": "2016-02-11T00:00:00", "origin": "MUA", "destination": "SFO", "emptySeats": 1, "plane": { "type": "Boeing 737", "totalSeats": 150 } }, { "ID": 6, "code": "ffee1112", "price": 954, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "CLE", "emptySeats": 100, "plane": { "type": "Boeing 787", "totalSeats": 200 } }, { "ID": 7, "code": "eefd1994", "price": 676, "departureDate": "2016-01-01T00:00:00", "origin": "MUA", "destination": "SFO", "emptySeats": 0, "plane": { "type": "Boeing 777", "totalSeats": 300 } }, { "ID": 8, "code": "ffee2000", "price": 300, "departureDate": "2016-02-20T00:00:00", "origin": "MUA", "destination": "SFO", "emptySeats": 30, "plane": { "type": "Boeing 737", "totalSeats": 150 } }, { "ID": 9, "code": "eefd3000", "price": 900, "departureDate": "2016-02-01T00:00:00", "origin": "MUA", "destination": "SFO", "emptySeats": 0, "plane": { "type": "Boeing 737", "totalSeats": 150 } }, { "ID": 10, "code": "eefd4511", "price": 900, "departureDate": "2016-01-15T00:00:00", "origin": "MUA", "destination": "LAX", "emptySeats": 100, "plane": { "type": "Boeing 777", "totalSeats": 300 } }, { "ID": 11, "code": "rree4567", "price": 456, "departureDate": "2016-01-20T00:00:00", "origin": "MUA", "destination": "SFO", "emptySeats": 100, "plane": { "type": "Boeing 737", "totalSeats": 150 } } ] -
Stop your app.
For guidance, see Stop Your App.
-
Proceed to Implementing the American Flights API Spec.




Cloud IDE
Desktop IDE