Mule Transformers
Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version. |
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.
Transformers in Mule fall into one of several categories, described in the table below.
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 |
Content |
This group of transformers modifies messages by adding to, deleting from, or converting a message payload. |
Append String |
SAP |
These transformers change SAP objects (JCo functions or IDoc documents) into XML representations, or vice versa. |
SAP-Object-to-XML |
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 |
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 |
Anypoint DataMapper Transformer
Beyond transformation, there is one type of message processor in Mule that both converts and maps data: the Anypoint DataMapper Transformer. In addition to transforming data from one format to another, DataMapper 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 DataMapper User Guide.
DataMapper Transformer Example
STUDIO Visual Editor
XML Editor or Standalone
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<sfdc:config name="Salesforce" username="salesforceuser@email.com" password="password" securityToken="8f9sFSD97jwifD7489df4LUU335" doc:name="Salesforce">
<sfdc:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>
</sfdc:config>
<data-mapper:config name="datamapper_grf" transformationGraphPath="datamapper.grf" doc:name="DataMapper"/>
<flow name="Contacts_to_SFDC" doc:name="Contacts_to_SFDC" doc:description="Upload a csv file of contact information into Salesforce as new contacts.">
<file:inbound-endpoint path="src/test/resources/input" moveToDirectory="src/test/resources/output" pollingFrequency="10000" responseTimeout="10000" doc:name="File Input"/>
<data-mapper:transform config-ref="datamapper_grf" doc:name="DataMapper"/>
<sfdc:create config-ref="Salesforce" type="Contact" doc:name="Salesforce">
<sfdc:objects ref="#[payload]"/>
</sfdc:create>
</flow>
</mule>
See Also
-
NEXT STEP: Read on about Filters, Scopes, and Routers.
-
Skip ahead to understand the structure of a Mule message.
-
Learn more about the DataMapper transformer.
-
Learn more about setting, then using properties and variables.