To Transform from a Full Set of Data to a Subset (Design Center)

In this procedure, you filter data that your app consumes from a REST service. When the flow executes, the code drills down into the full set of data, consisting of complete user profiles, and isolates just the user names. The key utility covered in this procedure is Transform. This utility transforms input from the complete profile of user data to only the user names. You can write the code yourself or use the visual mapper to generate the code. In this procedure, you use the visual mapper. You need to provide input and output data types for a successful mapping and code generation.

This procedure builds on the flows you built earlier to create a basic Mule application and to add an expression for dynamic requests.

To Create the Input Data Type

  1. In Design Center, after performing the procedure to dynamically change the URL of a request, proceed to the next step if the project is running. Otherwise, click Run. Execute the flow as follows:

    • Click options run app design env design center 2428b.

    • Click Copy Link.

    • Paste the link in a browser or client, and type /getpath?path=users at the end.

      The HTTP Listener hears the request, and starts the app. The JSON Placeholder profile of each user appears in the browser. The green status indicators appear in all parts of the flow.

  2. Copy the browser output, the user profiles, to the clipboard.

  3. In Design Center, at the end of the flow, click arrange cards flow design center e256e.

  4. Search for and select Transform.

  5. In the center pane, click Create New Data Type.

  6. In New Type, enter the following values:

    • Name: userlist

    • Format: JSON

    • Type: From example

    • In Add Your JSON Example Here, remove the {} and paste the browser output you copied.

      create userlist
    • Click Save.

To Create the Output Data Type

  1. Click Data Types + to create a second, output data type.

  2. In New Type, enter the following values:

    • Name: namelist

    • Format: JSON

    • Type: From Example

    • In Add Your JSON Example Here, Remove the default type and add this:

      ["john doe", "john smith"]

    • Click Save.

  3. In Input Payload, click payload options.

  4. Select Set.

  5. In Select Type, double-click userlist.

    The preview of the example you provided during creation of the data type appears.

  6. Click Select.

    The payload reflects the structure of the array data type.

  7. In Output Payload, click payload options, select Set, double-click the namelist type, and click Select.

  8. Click Mappings, and drag name String? from Input to item String in Output payload.

    Preview shows the list of names:

    map name to string

    DataWeave code is generated. Click Script to view the code:

    %dw 2.0
    output application/json
    (payload map (value0, index0) ->
  9. Click Run to execute the flow:

    • Click options run app design env design center 2428b, click Copy Link.

    • Paste the link in a browser or client, and type /getpath?path=users at the end.

      The list of names appears in the browser.

        "Leanne Graham",
        "Ervin Howell",
        "Clementine Bauch",
        "Patricia Lebsack",
        "Chelsey Dietrich",
        "Mrs. Dennis Schulist",
        "Kurtis Weissnat",
        "Nicholas Runolfsdottir V",
        "Glenna Reichert",
        "Clementina DuBuque"

Now, if you remove Transform from the flow, you are ready to drill down into JSON.