Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

Transforming Flight Data with DataWeave

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Use DataWeave to transform and match flight data to your API specification.

Create a User Snippet for the Transformation

Define user snippets in the mule-xml.json file. For information about user snippets, see Working with Code Snippets.

  1. Navigate to Configure User Snippets.

  2. In the Select Snippets File or Create Snippets field that opens, enter:

    mule-xml.json (Mule XML)
    command
  3. If the file has no configurations, mule-xml.json provides comments within curly braces:

    {
    	// Place your snippets for mule-xml here. Each snippet is defined under a snippet name and has a prefix, body and
    	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
    	// same ids are connected.
    	// Example:
    	// "Print to console": {
    	// 	"prefix": "log",
    	// 	"body": [
    	// 		"console.log('$1');",
    	// 		"$2"
    	// 	],
    	// 	"description": "Log output to console"
    	// }
    }
    json
  4. Add this user snippet code to the file:

    The user snippets provide starting configurations for several Mule components, including Transform Message.

  5. Proceed to Return the Payload as JSON.

Return the Payload as JSON

  1. In Anypoint Code Builder, open american-ws-anypoint-code-builder.xml.

  2. From the canvas, insert the user snippet Transform Message below the Database Select operation.

    After the Listener, click the (Add component) icon and navigate to Snippets > User Snippets > Transform Message.

    If the User Snippets panel is empty at first, wait a moment, and try again. Alternatively, navigate to the muledx:transform-message snippet from the configuration XML:

    Transform component highlighted
    User snippet XML:
    <ee:transform doc:name="" doc:id="51fed3-afee8c">
      <ee:message>
        <ee:set-payload>
          <![CDATA[]]>
        </ee:set-payload>
      </ee:message>
    </ee:transform>
    xml
  3. In the configuration XML, add the doc:name value Transform Message or similar to the <ee:transform/> component:

    <ee:transform doc:name="Transform Message" doc:id="423214-fac9c4">
    ...
  4. 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>
    dataweave
  5. Deploy your app within the IDE by selecting Run > Start Debugging (F5).

    For guidance, see Run Your App in Debug Mode.

  6. After the app deploys successfully, use a REST Client or browser to trigger a response.

    For guidance, see Test Your App.

  7. 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
      },
        ...
    ]
    JSON

    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.

  8. 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 ExchangeLeaving the Site.

API Specification Response Structure Actual Response Structure
[
  {
     "code": "ER38sd",
     "price": 400,
     "departureDate": "2017/07/26",
     "origin": "CLE",
     "destination": "SFO",
     "emptySeats": 0,
     "plane": {
       "type": "Boeing 737",
       "totalSeats": 150
     }
  }
]
json
[
  {
    "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
  }
]
json

Use DataWeave to transform the response:

  1. 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>
    xml
  2. Review your configuration XML:

  3. Proceed to Run and Test Your App.

Run and Test Your App

  1. Deploy your app within the IDE by executing to Run > Start Debugging (F5).

    For guidance, see Run Your App in Debug Mode.

  2. 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
            }
        },
        ...
    JSON
  3. Stop your app.

    For guidance, see Stop Your App.

  4. Proceed to Implementing the American Flights API Spec.