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
Anirban Sen Chowdhary
 
PPTX
Converting with custom transformer
Anirban Sen Chowdhary
 
PPTX
Converting with custom transformer
irfan1008
 
PPTX
Mule esb How to use Jackson in Object to Json converter
Raffaela Vitiello
 
PPTX
Mule parsing with json
Anirban Sen Chowdhary
 
PPTX
Mule esb How to convert from Object to Json in 5 minutes
Gennaro Spagnoli
 
PPTX
Mule esb How to use Jackson in Json to Object converter
Gennaro Spagnoli
 
PPTX
Mule esb - How to convert from Json to Object in 5 minutes
Gennaro Spagnoli
 
Converting with custom transformer part 2
Anirban Sen Chowdhary
 
Converting with custom transformer
Anirban Sen Chowdhary
 
Converting with custom transformer
irfan1008
 
Mule esb How to use Jackson in Object to Json converter
Raffaela Vitiello
 
Mule parsing with json
Anirban Sen Chowdhary
 
Mule esb How to convert from Object to Json in 5 minutes
Gennaro Spagnoli
 
Mule esb How to use Jackson in Json to Object converter
Gennaro Spagnoli
 
Mule esb - How to convert from Json to Object in 5 minutes
Gennaro Spagnoli
 

What's hot (9)

PPTX
Mule parsing with json part2
Anirban Sen Chowdhary
 
PPTX
Mule esb object_to_jackson_json
Davide Rapacciuolo
 
PPTX
Mule esb object_to_json
Davide Rapacciuolo
 
PPTX
Mule esb object_to_json
Davide Rapacciuolo
 
PPTX
Muleesbobjecttojson
Davide Rapacciuolo
 
PPTX
Mule esb json_to_object
Davide Rapacciuolo
 
PPTX
Web Services
Victor Montalvão
 
PPTX
Mapping and listing in mule
Khan625
 
PPTX
Mapping and listing with mule
Praneethchampion
 
Mule parsing with json part2
Anirban Sen Chowdhary
 
Mule esb object_to_jackson_json
Davide Rapacciuolo
 
Mule esb object_to_json
Davide Rapacciuolo
 
Mule esb object_to_json
Davide Rapacciuolo
 
Muleesbobjecttojson
Davide Rapacciuolo
 
Mule esb json_to_object
Davide Rapacciuolo
 
Web Services
Victor Montalvão
 
Mapping and listing in mule
Khan625
 
Mapping and listing with mule
Praneethchampion
 
Ad

Similar to Converting with custom transformer (20)

PPTX
Converting with custom transformer
Sunil Komarapu
 
PPTX
Converting with custom transformer
Hasan Syed
 
PPTX
Mule esb How to convert from Json to Xml in 5 minutes
Gennaro Spagnoli
 
PPTX
Xml to xml transformation in mule
javeed_mhd
 
PPTX
Xml to xml transformation in mule
Rajkattamuri
 
PPTX
Xml to xml transformation in mule
Mohammed625
 
PPTX
Json to xml esb transformation
Domenico Schiavone
 
PPTX
Mule xml transformation
D.Rajesh Kumar
 
PPTX
Mule ESB - How to convert from Xml to Json in 5 minutes
Gennaro Spagnoli
 
PPTX
Xml transform
Son Nguyen
 
PPTX
Xml to xml transformation in mule
mdfkhan625
 
PPTX
Xml to xml transformation
Son Nguyen
 
PPTX
Xml to xml transformation in mule
Davide Rapacciuolo
 
PPTX
Xml to xml transformation in mule
Germano Barba
 
PPTX
Xml to xml transformation in mule
Davide Rapacciuolo
 
PPTX
Using XSLT in Mule
Anirban Sen Chowdhary
 
PPTX
Xml toobjectesbtransform
Domenico Schiavone
 
PPTX
Json
manavp
 
PPTX
Xslt in mule
Sunil Komarapu
 
PPTX
Xslt in mule
Shahid Shaik
 
Converting with custom transformer
Sunil Komarapu
 
Converting with custom transformer
Hasan Syed
 
Mule esb How to convert from Json to Xml in 5 minutes
Gennaro Spagnoli
 
Xml to xml transformation in mule
javeed_mhd
 
Xml to xml transformation in mule
Rajkattamuri
 
Xml to xml transformation in mule
Mohammed625
 
Json to xml esb transformation
Domenico Schiavone
 
Mule xml transformation
D.Rajesh Kumar
 
Mule ESB - How to convert from Xml to Json in 5 minutes
Gennaro Spagnoli
 
Xml transform
Son Nguyen
 
Xml to xml transformation in mule
mdfkhan625
 
Xml to xml transformation
Son Nguyen
 
Xml to xml transformation in mule
Davide Rapacciuolo
 
Xml to xml transformation in mule
Germano Barba
 
Xml to xml transformation in mule
Davide Rapacciuolo
 
Using XSLT in Mule
Anirban Sen Chowdhary
 
Xml toobjectesbtransform
Domenico Schiavone
 
Json
manavp
 
Xslt in mule
Sunil Komarapu
 
Xslt in mule
Shahid Shaik
 
Ad

More from mdfkhan625 (20)

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

Recently uploaded (20)

PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Software Development Methodologies in 2025
KodekX
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 

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