SlideShare a Scribd company logo
Transforming with Custom
Transformer in Mule
Sometime in our Mule flow we require to transform a
payload from one form to another.
For example in some cases, we need transform an XML
payload to JSON
Now there are several ways of transforming the XML payload to
JSON in Mule. You can use XML to Object transformer and then
Object to JSON transformer in doing so.
But how about using a custom transformer to directly transform
XML to JSON ?
It will be a very easy way in achieving that without using much
transformer in our flow and can directly transform end-to-end
But how can we use Custom transformer to transform in
Mule??
.
Here I will show you how ……
Let us consider we have a following Mule flow :-
Now you can see in the above flow the inbound endpoint will pic a file that
contains XML content from a location and put it into another location and the
outbound file will contain it’s corresponding JSON content.
You can also see a custom transformer in the middle which is responsible for
this XML payload conversion to JSON directly.
Now, let’s check the code for this flow :-
<flow name="CustomXMLToJSONTransformer" doc:name="CustomXMLToJSONTransformer">
<file:inbound-endpoint path="E:backuptest" responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="xmlFile.txt" caseSensitive="false"/>
</file:inbound-endpoint>
<file:file-to-string-transformer doc:name="File to String"/>
<custom-transformer class="CustomXMLToJSONTransformer" doc:name="XmlToJson"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="E:backuptestnewfolder" outputPattern="jsonFile.txt"
responseTimeout="10000" doc:name="File" />
</flow>
As you can see the file xmlFile.txt contains XML content that will be converted directly
into JSON by the custom-transformer into a file jsonFile.txt
Now let’s check the XML content of file xmlFile.txt :-
So, the above is the XML content we need to convert using our custom
transformer into a corresponding JSON
So our custom transformer java class is the following :-
public class CustomXMLToJSONTransformer extends AbstractMessageTransformer implements
DiscoverableTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
String xml = (String) message.getPayload();
XmlMapper xmlMapper = new XmlMapper();
List entries = xmlMapper.readValue(xml, List.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(entries);
return json;
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
return null;
}
@Override
public int getPriorityWeighting() {
return 0;
}
@Override
public void setPriorityWeighting(int weighting) {
}
}
Now let’s test our application . We will see the following in our Mule console :-
You can see the payload is transformed into JSON and has been dispatched to location
E:backuptestnewfolder with file name jsonFile.txt
Now if we open the file jsonFile.txt from location
E:backuptestnewfolder we will get our JSON content as following
You can see you have generated the JSON for the XML directly just using a simple
custom transformer component
Hope you enjoyed the simple yet an amazing trick in Mule
Converting with custom transformer

More Related Content

PPTX
Converting with custom transformer part 2
PPTX
Converting with custom transformer
PPTX
Converting with custom transformer
PPTX
Mule esb How to use Jackson in Object to Json converter
PPTX
Mule parsing with json
PPTX
Mule esb How to convert from Object to Json in 5 minutes
PPTX
Mule esb How to use Jackson in Json to Object converter
PPTX
Mule esb - How to convert from Json to Object in 5 minutes
Converting with custom transformer part 2
Converting with custom transformer
Converting with custom transformer
Mule esb How to use Jackson in Object to Json converter
Mule parsing with json
Mule esb How to convert from Object to Json in 5 minutes
Mule esb How to use Jackson in Json to Object converter
Mule esb - How to convert from Json to Object in 5 minutes

What's hot (9)

PPTX
Mule parsing with json part2
PPTX
Mule esb object_to_jackson_json
PPTX
Mule esb object_to_json
PPTX
Mule esb object_to_json
PPTX
Muleesbobjecttojson
PPTX
Mule esb json_to_object
PPTX
Web Services
PPTX
Mapping and listing in mule
PPTX
Mapping and listing with mule
Mule parsing with json part2
Mule esb object_to_jackson_json
Mule esb object_to_json
Mule esb object_to_json
Muleesbobjecttojson
Mule esb json_to_object
Web Services
Mapping and listing in mule
Mapping and listing with mule
Ad

Similar to Converting with custom transformer (20)

PPTX
Converting with custom transformer
PPTX
Converting with custom transformer
PPTX
Xml to xml transformation
PPTX
Xml to xml transformation in mule
PPTX
Using XSLT in Mule
PPTX
Json to json transformation in mule
PPTX
Transformation jsontojsonesb
PPTX
Transformation jsontojsonesb
PPTX
Xml to xml transformation in mule
PPTX
Xml to xml transformation in mule
PPTX
Xml to xml transformation in mule
PPTX
Xslt with mule
PPTX
Xslt in mule
PPTX
Xslt in mule
PPTX
Xslt in mule
PPTX
Xslt in mule
PPTX
Xml transform
PPTX
Using xslt in mule
PPTX
Using xslt in mule
PPTX
Using xslt in mule
Converting with custom transformer
Converting with custom transformer
Xml to xml transformation
Xml to xml transformation in mule
Using XSLT in Mule
Json to json transformation in mule
Transformation jsontojsonesb
Transformation jsontojsonesb
Xml to xml transformation in mule
Xml to xml transformation in mule
Xml to xml transformation in mule
Xslt with mule
Xslt in mule
Xslt in mule
Xslt in mule
Xslt in mule
Xml transform
Using xslt in mule
Using xslt in mule
Using xslt in mule
Ad

More from mdfkhan625 (20)

PPTX
Mapping and listing with mule
PPTX
How to use message properties component
PPTX
How to use expression filter
PPTX
Data weave
PPT
Anypoint data gateway
PPTX
Webservice with vm in mule
PPTX
Validating soap request in mule
PPTX
Groovy example in mule
PPTX
Scatter gather flow control
PPTX
Mule with velocity
PPTX
Mule with rabbit mq
PPTX
Mule with quartz
PPTX
Mule with drools
PPTX
Mule esb
PPTX
Idempotent filter with simple file
PPTX
Creating dynamic json
PPTX
Caching and invalidating with managed store
PPTX
Cache for community edition
PPTX
Automatic documantation with mule
PPTX
Webservice with vm
Mapping and listing with mule
How to use message properties component
How to use expression filter
Data weave
Anypoint data gateway
Webservice with vm in mule
Validating soap request in mule
Groovy example in mule
Scatter gather flow control
Mule with velocity
Mule with rabbit mq
Mule with quartz
Mule with drools
Mule esb
Idempotent filter with simple file
Creating dynamic json
Caching and invalidating with managed store
Cache for community edition
Automatic documantation with mule
Webservice with vm

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Big Data Technologies - Introduction.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
Chapter 3 Spatial Domain Image Processing.pdf
sap open course for s4hana steps from ECC to s4
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release

Converting with custom transformer

  • 2. Sometime in our Mule flow we require to transform a payload from one form to another. For example in some cases, we need transform an XML payload to JSON
  • 3. Now there are several ways of transforming the XML payload to JSON in Mule. You can use XML to Object transformer and then Object to JSON transformer in doing so. But how about using a custom transformer to directly transform XML to JSON ? It will be a very easy way in achieving that without using much transformer in our flow and can directly transform end-to-end
  • 4. But how can we use Custom transformer to transform in Mule?? .
  • 5. Here I will show you how ……
  • 6. Let us consider we have a following Mule flow :- Now you can see in the above flow the inbound endpoint will pic a file that contains XML content from a location and put it into another location and the outbound file will contain it’s corresponding JSON content. You can also see a custom transformer in the middle which is responsible for this XML payload conversion to JSON directly.
  • 7. Now, let’s check the code for this flow :- <flow name="CustomXMLToJSONTransformer" doc:name="CustomXMLToJSONTransformer"> <file:inbound-endpoint path="E:backuptest" responseTimeout="10000" doc:name="File"> <file:filename-regex-filter pattern="xmlFile.txt" caseSensitive="false"/> </file:inbound-endpoint> <file:file-to-string-transformer doc:name="File to String"/> <custom-transformer class="CustomXMLToJSONTransformer" doc:name="XmlToJson"/> <logger message="#[message.payload]" level="INFO" doc:name="Logger" /> <file:outbound-endpoint path="E:backuptestnewfolder" outputPattern="jsonFile.txt" responseTimeout="10000" doc:name="File" /> </flow> As you can see the file xmlFile.txt contains XML content that will be converted directly into JSON by the custom-transformer into a file jsonFile.txt
  • 8. Now let’s check the XML content of file xmlFile.txt :- So, the above is the XML content we need to convert using our custom transformer into a corresponding JSON
  • 9. So our custom transformer java class is the following :- public class CustomXMLToJSONTransformer extends AbstractMessageTransformer implements DiscoverableTransformer { public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { try { String xml = (String) message.getPayload(); XmlMapper xmlMapper = new XmlMapper(); List entries = xmlMapper.readValue(xml, List.class); ObjectMapper jsonMapper = new ObjectMapper(); String json = jsonMapper.writeValueAsString(entries); return json; } catch (Exception e) { System.out.println("Error: " + e); e.printStackTrace(); } return null; } @Override public int getPriorityWeighting() { return 0; } @Override public void setPriorityWeighting(int weighting) { } }
  • 10. Now let’s test our application . We will see the following in our Mule console :- You can see the payload is transformed into JSON and has been dispatched to location E:backuptestnewfolder with file name jsonFile.txt
  • 11. Now if we open the file jsonFile.txt from location E:backuptestnewfolder we will get our JSON content as following You can see you have generated the JSON for the XML directly just using a simple custom transformer component
  • 12. Hope you enjoyed the simple yet an amazing trick in Mule