Nav

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 data types for a successful mapping and code generation.

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

  1. Proceed to the next step if myproject is running. Otherwise, click Deploy. 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?keyword=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 indicator appears in all parts of the flow.

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

  3. Click arrange cards flow design center e256e.

  4. Select Transform, and in Input, click Set Data Type set data type.

  5. Create a new data type:

    • 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 Apply Changes.

  6. In Output Payload, click Set Data Type.

    set output datatype
  7. Create a new data type for the output:

    • Name: namelist

    • Format: JSON

    • Type: From Example

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

      ["john does", "john smith"]

    • Click Apply Changes.

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

    The 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) -> value0.name)
  9. Click Deploy, and execute the flow:

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

    • When deployment is complete, 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.

In this topic: