SlideShare a Scribd company logo
1 of 13
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

What's hot

Converting with custom transformer part 2
Converting with custom transformer part 2Converting with custom transformer part 2
Converting with custom transformer part 2Anirban Sen Chowdhary
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in muleAntonio Pellegrino
 
Mule esb How to use Jackson in Object to Json converter
Mule esb How to use Jackson in Object to Json converterMule esb How to use Jackson in Object to Json converter
Mule esb How to use Jackson in Object to Json converterRaffaela Vitiello
 
Mule esb How to use Jackson in Json to Object converter
Mule esb How to use Jackson in Json to Object converterMule esb How to use Jackson in Json to Object converter
Mule esb How to use Jackson in Json to Object converterGennaro Spagnoli
 

What's hot (6)

Converting with custom transformer part 2
Converting with custom transformer part 2Converting with custom transformer part 2
Converting with custom transformer part 2
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
 
Mule esb How to use Jackson in Object to Json converter
Mule esb How to use Jackson in Object to Json converterMule esb How to use Jackson in Object to Json converter
Mule esb How to use Jackson in Object to Json converter
 
Mule esb How to use Jackson in Json to Object converter
Mule esb How to use Jackson in Json to Object converterMule esb How to use Jackson in Json to Object converter
Mule esb How to use Jackson in Json to Object converter
 
Mule requesterdemo
Mule requesterdemoMule requesterdemo
Mule requesterdemo
 

Viewers also liked

Viewers also liked (14)

Comercio electrónico
Comercio electrónicoComercio electrónico
Comercio electrónico
 
John keynes
John keynesJohn keynes
John keynes
 
Fault tolerance
Fault toleranceFault tolerance
Fault tolerance
 
Mule with rabbit mq
Mule with rabbit mqMule with rabbit mq
Mule with rabbit mq
 
VenkataMaheswari_Resume
VenkataMaheswari_ResumeVenkataMaheswari_Resume
VenkataMaheswari_Resume
 
Harbin Dadi E-catalog
Harbin Dadi E-catalog Harbin Dadi E-catalog
Harbin Dadi E-catalog
 
Automotive catalogue
Automotive catalogueAutomotive catalogue
Automotive catalogue
 
Recursamiento diseña-y-administra-plataformas-e-learning
Recursamiento diseña-y-administra-plataformas-e-learningRecursamiento diseña-y-administra-plataformas-e-learning
Recursamiento diseña-y-administra-plataformas-e-learning
 
Prévia da inflação de setembro no Brasil
Prévia da inflação de setembro no BrasilPrévia da inflação de setembro no Brasil
Prévia da inflação de setembro no Brasil
 
Catalogo Nolo And Service - Noleggio Attrezzatura industriale
Catalogo Nolo And Service - Noleggio Attrezzatura industrialeCatalogo Nolo And Service - Noleggio Attrezzatura industriale
Catalogo Nolo And Service - Noleggio Attrezzatura industriale
 
Shamal Solutions resume
Shamal Solutions resumeShamal Solutions resume
Shamal Solutions resume
 
E learning
E learningE learning
E learning
 
El marginalismo
El marginalismoEl marginalismo
El marginalismo
 
Moss-Adams_2012-Corp-Social-Responsibility-Report
Moss-Adams_2012-Corp-Social-Responsibility-ReportMoss-Adams_2012-Corp-Social-Responsibility-Report
Moss-Adams_2012-Corp-Social-Responsibility-Report
 

Similar to Converting with custom transformer

Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformerjaveed_mhd
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformerMohammed246
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer AbdulImrankhan7
 
Converting with custom transforme
Converting with custom transformeConverting with custom transforme
Converting with custom transformeKhasim Saheb
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer mdfkhan625
 
Transforming with custom transformer in mule
Transforming with custom transformer in muleTransforming with custom transformer in mule
Transforming with custom transformer in mulePraneethchampion
 
Xml to xml transformation
Xml to xml transformationXml to xml transformation
Xml to xml transformationSon Nguyen
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mulemdfkhan625
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesbGermano Barba
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in muleRajkattamuri
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mulejaveed_mhd
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in muleMohammed625
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in muleDavide Rapacciuolo
 
Xslt with mule
Xslt with muleXslt with mule
Xslt with muleSon Nguyen
 

Similar to Converting with custom transformer (20)

Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer
 
Converting with custom transforme
Converting with custom transformeConverting with custom transforme
Converting with custom transforme
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer
 
Transforming with custom transformer in mule
Transforming with custom transformer in muleTransforming with custom transformer in mule
Transforming with custom transformer in mule
 
Xml to xml transformation
Xml to xml transformationXml to xml transformation
Xml to xml transformation
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
 
Using XSLT in Mule
Using XSLT in MuleUsing XSLT in Mule
Using XSLT in Mule
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
 
Xslt with mule
Xslt with muleXslt with mule
Xslt with mule
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
 

More from Hasan Syed

Web service vm in mule
Web service vm in muleWeb service vm in mule
Web service vm in muleHasan Syed
 
Validate soap request in mule
Validate soap request in muleValidate soap request in mule
Validate soap request in muleHasan Syed
 
Simple web service vm
Simple web service vmSimple web service vm
Simple web service vmHasan Syed
 
Scatter gather flow control
Scatter gather flow controlScatter gather flow control
Scatter gather flow controlHasan Syed
 
Mule with velocity
Mule with velocityMule with velocity
Mule with velocityHasan Syed
 
Mule with quartz
Mule with quartzMule with quartz
Mule with quartzHasan Syed
 
Mule with drools
Mule with droolsMule with drools
Mule with droolsHasan Syed
 
Idempotent filter with simple file
Idempotent filter with simple fileIdempotent filter with simple file
Idempotent filter with simple fileHasan Syed
 
Groovy with mule
Groovy with muleGroovy with mule
Groovy with muleHasan Syed
 
Creating dynamic json
Creating dynamic jsonCreating dynamic json
Creating dynamic jsonHasan Syed
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed storeHasan Syed
 
Cache for community edition
Cache for community editionCache for community edition
Cache for community editionHasan Syed
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with muleHasan Syed
 

More from Hasan Syed (20)

Web service vm in mule
Web service vm in muleWeb service vm in mule
Web service vm in mule
 
Validate soap request in mule
Validate soap request in muleValidate soap request in mule
Validate soap request in mule
 
Simple web service vm
Simple web service vmSimple web service vm
Simple web service vm
 
Scatter gather flow control
Scatter gather flow controlScatter gather flow control
Scatter gather flow control
 
Mule with velocity
Mule with velocityMule with velocity
Mule with velocity
 
Mule with quartz
Mule with quartzMule with quartz
Mule with quartz
 
Mule with drools
Mule with droolsMule with drools
Mule with drools
 
Mule esb
Mule esbMule esb
Mule esb
 
Maven iii
Maven iiiMaven iii
Maven iii
 
Maven ii
Maven iiMaven ii
Maven ii
 
Maven part 1
Maven part 1Maven part 1
Maven part 1
 
Jenkins3
Jenkins3Jenkins3
Jenkins3
 
Jenkins2
Jenkins2Jenkins2
Jenkins2
 
Jenkins
JenkinsJenkins
Jenkins
 
Idempotent filter with simple file
Idempotent filter with simple fileIdempotent filter with simple file
Idempotent filter with simple file
 
Groovy with mule
Groovy with muleGroovy with mule
Groovy with mule
 
Creating dynamic json
Creating dynamic jsonCreating dynamic json
Creating dynamic json
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed store
 
Cache for community edition
Cache for community editionCache for community edition
Cache for community edition
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with mule
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

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