Contact Us 1-800-596-4880

Transformers

transformer

In a Mule flow, a transformer prepares a message for further processing by enhancing or altering the contents of the message properties, variables, or payload. Data transformation is one of the most powerful functionalities of Mule: rather than spending a lot of time building a customized connection between resources or processors, you can just use a pre-built transformer to perform a standard data conversion. For example, if the message source in a flow receives data in XML format, but a downstream message processor expects a Java object, you can use an XML-to-Object transformer to convert the format of the message payload.

Transformer Library

Out of the box, Mule provides a set of standard transformers to handle the most common data transformation scenarios. Typically, these elements require minimal configuration so as to facilitate quick construction of applications that must juggle different data formats between resources and processors. If Mule doesn’t have the particular transformer you need, you can arrange several transformers in a sequence to achieve the output you need. For example, if you need to implement an A-to-C transformation but no such transformer exists, you can arrange a sequence – A-to-B, B-to-C – which effectively simulates an A-to-C transformer. For example, to convert XML to JSON, use an XML-to-Object transformer followed by an Object-to-JSON transformer.

Examples of transformers in Mule fall into these categories:

Transformers Category Description Example Transformers

Java Object

This category contains the bulk of the Mule transformers. Each transformer in this group changes a Java object into another Java object, a Java object into a non-Java data type (such as an HTTP request), or vice versa.

  • JSON to Object

  • XML to Object

Content

This group of transformers modifies messages by adding to, deleting from, or converting a message payload.

  • Append String

  • Expression

  • Parse Template

  • XSLT

SAP

These transformers change SAP objects (JCo functions or IDoc documents) into XML representations, or vice versa.

  • SAP Object to XML

  • XML to SAP Function (BAPI)

  • XML to SAP IDoc

Script

This type of transformer utilizes a custom script to perform the transformation. If you can’t find the transformer you need in the above-listed categories, you use one of these transformers to implement a custom script to perform the transformation. Just add one of these to your flow, then write a script in your favorite language to convert data as needed.

  • Groovy

  • JavaScript

  • Python

  • Ruby

  • Script

Properties, Variables, and Attachments

Rather than acting upon the message payload, these transformers add, remove, or copy properties, variables, and attachments on the message. This group of message processors doesn’t so much transform as manipulate or enrich the contents of the message header.

  • Attachment

  • Property

  • Session Variable

  • Variable

DataWeave Transformer

Beyond transformation, there is one type of message processor in Mule that both converts and maps data: the DataWeave Transformer, labeled as Transform Message in the palette. In addition to transforming data from one format to another, DataWeave can map an input field, such as last_name, to a different output field, such as family_name (see image below). Even better, it can map multiple fields, such as title, first_name, and last_name, to a composite output field such as full_name. It can retrieve session state information in a message to facilitate conditional message routing, it can use Mule expression evaluation to facilitate conditional value recalculation, it can even look up information in tables or other flows. Read more about it in the DataWeave.

Transform the Payload to a Java Class

Mule’s auto-transformation capability allows you to retrieve the message payload in a particular format, by converting the message payload to a Java Class that you define.
The payloadAs(<class>) method applies a mule transformer to the payload and converts it to the specified Java class. For example, the MEL expression #[message.payloadAs(java.lang.String)] transforms the message’s payload to String.

The expression`#[message.payloadAs(${javaClass})]` does not alter your payload unless the payload is an InputStream. In this case, the payload becomes the entire inputStream when converted to the specified class.
Also, if your message is null Mule will convert it to a Java Class only if said Class supports null types. Otherwise, Mule returns a TransformerException.

See Also