Nav

Transform Utility Reference (Design Center)

To map inputs to outputs and then generate DataWeave code, you might need to provide input and output data type definitions for the Transform utility. From this information Transform can infer the structure of the data. In some cases, Transform can detect the structure of the input. In this case, the structure appears in Input after you perform a mock request: no need to define a data type.

If Transform cannot detect the structure of the data, you define the data type of the input or output data you expect. The definition consists of the format, such as JSON or CSV, and an example of the data.

Whether or not Transform can infer a structure is fairly predictable. For example, when the input to Transform comes from the database connector, the data structure is already defined; no need to define a data type. When the input to Transform is from the HTTP listener, the payload structure is unpredictable and you need to define a data type.

Transforming Multiple Output Parts

In Output, in Mappings you can click Output Payload > Add Transformation to transform multiple parts of the output of a single Transform utility. You need to define separate DataWeave code for each part of the output. For example: if one tab defines the payload, and another a variable, both are parts of the same Mule message output. Execution order of multiple outputs might vary.

Debugging and Changing DataWeave Code

You can preview the sample output of the DataWeave code to see if the code produces the transformation you expect. You can preview sample input data to your Transform component to see how the transformation affects it. You can also set a fixed value or function on the graphical map to enhance or adjust the code.

Previewing the Transformation

You can preview the transformation using sample input data. You don’t need to run your application to preview the transformation. Sample output is updated in real time as you make changes to the sample input and the transformation.

A preview appears when you click Edit Sample Data in Input preview transformation output design center 1a65a.

On Input, you select one of the parts of the input: Payload, Attributes, or Variable. Clicking the Edit Sample Data icon preview transformation output design center 1a65a populates the sample data.

  • If you defined your Data Type through a sample, the values you used in the sample data appear in the preview.

  • If you defined your Data Type through a schema or it was automatically defined, values are populated with the string ???? as a placeholder.

    As you make changes in the sample data or the transformation, the output preview changes. The undetectable values (????) are replaced by sample values when the transformation works with the sample data provided.

If the transformation results in multiple outputs (for example, the payload and a variable), the preview shows the output of the currently selected transformation.

When the output is of type POJO (Plain Old Java Object), the preview is in JSON format.

POJO Input

When the input is of type POJO (Plain Old Java Object), you need to write samples in DataWeave format to express how data is structured, as shown in the following example:

[
  {
      message @(id: "FSD-483"): "Hello World!",
      date: |2017-05-06|,
      items: {
          item1: 434,
          item2: "344222"
      }
  },
  {
      message @(id: "DSF-829"): "This is what DataWeave format looks like.",
      date: |2017-03-21|,
      items: {
          item1: 728,
          item2: "14422"
      }
  }
]

In Transform, in Input, you enter this code using Edit Sample Data preview transformation output design center 1a65a.

You do not use this code as the DataWeave transformation script. The sample you define works only within the Transform utility and does not affect any other part of the application.

Setting a Fixed Value or Function

You can control the transformation of particular fields by explicitly assigning a value or function to the field in the mapper.

Double-click a field having no children in the output to add graphically construct mapping design center task 8a822 to the tree.

In the text box, which contains null by default, you can enter the following types of values:

  • A fixed text value by using " ".

  • A reference to an input field.

  • A statement that includes DataWeave functions.

Similar to using a spreadsheet, each field can directly reference another field or be the product of a function that involves references.