SlideShare a Scribd company logo
By Anirban Sen Chowdhary
.
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 JSON content from a location and put it into
another location and the outbound file will contain it’s
corresponding XML content.
You can also see a custom transformer in the middle which is
responsible for this JSON payload conversion to XML directly.
Now, let’s check the code for this flow :-
<flow name="CustomJSONtoXMLTransformer"
doc:name="CustomJSONtoXMLTransformer">
<file:inbound-endpoint path="E:backuptestnewfolder"
responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="jsonFile.txt" caseSensitive="false"/>
</file:inbound-endpoint>
<file:file-to-string-transformer doc:name="File to String"/>
<custom-transformer class="CustomJSONtoXMLTransformer"
doc:name="JsonToXml"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="E:backuptest" outputPattern="xmlFile.txt"
responseTimeout="10000" doc:name="File" />
</flow>
As you can see the file jsonFile.txt contains JSON content that will be
converted directly into XML by the custom-transformer into a file
xmlFile.txt
Now let’s check the JSON content of file jsonFile.txt :-
So, the above is the JSON content we need to convert using our
custom transformer into a corresponding XML
So our custom transformer java class is the following :-
public class CustomJSONtoXMLTransformer extends AbstractMessageTransformer implements
DiscoverableTransformer {
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String jsonStr = (String) message.getPayload();
JSONObject jsonObj;
try {
jsonObj = new JSONObject(jsonStr);
System.out.println(XML.toString(jsonObj));
//To append <?xml version="1.0"?>
/* StringBuilder builder = new StringBuilder();
builder
.append("<?xml version="1.0"?>")
.append(XML.toString(jsonObj));
String xml_version= XML.toString(jsonObj);
xml_version= builder.toString();
return xml_version;*/
//Append ends
return XML.toString(jsonObj);
}
catch (JSONException 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 XML and has been
dispatched to location E:backuptest with file name xmlFile.txt
Now if we open the file xmlFile.txt from location
E:backuptest we will get our XML content as following
You can see you have generated the XML for the JSON directly just
using a simple custom transformer component
Hope you enjoyed the simple yet an amazing trick in Mule
Converting with custom transformer part 2

More Related Content

What's hot

.NET/C#_19
.NET/C#_19.NET/C#_19
.NET/C#_19
RaynaITSTEP
 
Promises, promises, and then observables
Promises, promises, and then observablesPromises, promises, and then observables
Promises, promises, and then observables
Stefan Charsley
 
VB 6
VB 6VB 6
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
Janssen Harvey Insigne
 
Json.parse() in JavaScript
Json.parse() in JavaScriptJson.parse() in JavaScript
Json.parse() in JavaScript
Ideas2IT Technologies
 
AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals
Adam Book
 
An Introduction to WebWorker - 01.26.12
An Introduction to WebWorker - 01.26.12An Introduction to WebWorker - 01.26.12
An Introduction to WebWorker - 01.26.12
Digiflare
 
Ete programs
Ete programsEte programs
Ete programs
Mayur Wankhede
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
MongoDB
 
Constructor in detail
Constructor in detailConstructor in detail
Constructor in detail
HSS-Software House
 
Additional action script 3.0
Additional action script 3.0Additional action script 3.0
Additional action script 3.0
Brian Kelly
 

What's hot (11)

.NET/C#_19
.NET/C#_19.NET/C#_19
.NET/C#_19
 
Promises, promises, and then observables
Promises, promises, and then observablesPromises, promises, and then observables
Promises, promises, and then observables
 
VB 6
VB 6VB 6
VB 6
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
 
Json.parse() in JavaScript
Json.parse() in JavaScriptJson.parse() in JavaScript
Json.parse() in JavaScript
 
AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals
 
An Introduction to WebWorker - 01.26.12
An Introduction to WebWorker - 01.26.12An Introduction to WebWorker - 01.26.12
An Introduction to WebWorker - 01.26.12
 
Ete programs
Ete programsEte programs
Ete programs
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
 
Constructor in detail
Constructor in detailConstructor in detail
Constructor in detail
 
Additional action script 3.0
Additional action script 3.0Additional action script 3.0
Additional action script 3.0
 

Similar to Converting with custom transformer part 2

Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Anirban Sen Chowdhary
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
javeed_mhd
 
Converting with custom transforme
Converting with custom transformeConverting with custom transforme
Converting with custom transforme
Khasim Saheb
 
Transforming with custom transformer in mule
Transforming with custom transformer in muleTransforming with custom transformer in mule
Transforming with custom transformer in mule
Praneethchampion
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer
mdfkhan625
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Mohammed246
 
Converting with custom transformer
Converting with custom transformer Converting with custom transformer
Converting with custom transformer
AbdulImrankhan7
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Hasan Syed
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Sunil Komarapu
 
JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2
Laurence Svekis ✔
 
Validating a json in mule
Validating a json in muleValidating a json in mule
Validating a json in mule
Anirban Sen Chowdhary
 
Php sql-android
Php sql-androidPhp sql-android
Php sql-android
maamir farooq
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
dyumna2
 
Json parser
Json parserJson parser
Json parser
Son Nguyen
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problems
titanlambda
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
Antonio Pellegrino
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
Germano Barba
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
Antonio Pellegrino
 
Web Services
Web ServicesWeb Services
Web Services
Victor Montalvão
 
Json to json esb transformation
Json to json esb transformationJson to json esb transformation
Json to json esb transformation
Domenico Schiavone
 

Similar to Converting with custom transformer part 2 (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 transforme
Converting with custom transformeConverting with custom transforme
Converting with custom transforme
 
Transforming with custom transformer in mule
Transforming with custom transformer in muleTransforming with custom transformer in mule
Transforming with custom transformer in mule
 
Converting with custom transformer
Converting with custom transformer Converting 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 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
 
JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2
 
Validating a json in mule
Validating a json in muleValidating a json in mule
Validating a json in mule
 
Php sql-android
Php sql-androidPhp sql-android
Php sql-android
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
 
Json parser
Json parserJson parser
Json parser
 
JSON Fuzzing: New approach to old problems
JSON Fuzzing: New  approach to old problemsJSON Fuzzing: New  approach to old problems
JSON Fuzzing: New approach to old problems
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
 
Web Services
Web ServicesWeb Services
Web Services
 
Json to json esb transformation
Json to json esb transformationJson to json esb transformation
Json to json esb transformation
 

More from Anirban Sen Chowdhary

Change the game with Game changer
Change the game with Game changerChange the game with Game changer
Change the game with Game changer
Anirban Sen Chowdhary
 
Ring central desktop app overview
Ring central desktop app overviewRing central desktop app overview
Ring central desktop app overview
Anirban Sen Chowdhary
 
Overview in ringcentral digital line
Overview in ringcentral digital lineOverview in ringcentral digital line
Overview in ringcentral digital line
Anirban Sen Chowdhary
 
Some basics with ring central
Some basics with ring centralSome basics with ring central
Some basics with ring central
Anirban Sen Chowdhary
 
Ring central and python
Ring central and pythonRing central and python
Ring central and python
Anirban Sen Chowdhary
 
RingCentral application development overview
RingCentral application development overviewRingCentral application development overview
RingCentral application development overview
Anirban Sen Chowdhary
 
Cloze connect ringcentral
Cloze connect ringcentralCloze connect ringcentral
Cloze connect ringcentral
Anirban Sen Chowdhary
 
Overview on ring central errors part 4
Overview on ring central errors part 4Overview on ring central errors part 4
Overview on ring central errors part 4
Anirban Sen Chowdhary
 
Setting up your ring central sandbox in steps
Setting up your ring central sandbox in stepsSetting up your ring central sandbox in steps
Setting up your ring central sandbox in steps
Anirban Sen Chowdhary
 
Overview on ring central errors: part 2
Overview on ring central errors: part 2Overview on ring central errors: part 2
Overview on ring central errors: part 2
Anirban Sen Chowdhary
 
Overview on ring central errors
Overview on ring central errorsOverview on ring central errors
Overview on ring central errors
Anirban Sen Chowdhary
 
Call recording overview ring central
Call recording overview  ring centralCall recording overview  ring central
Call recording overview ring central
Anirban Sen Chowdhary
 
Ring central engaging with amazon alexa
Ring central engaging with amazon alexaRing central engaging with amazon alexa
Ring central engaging with amazon alexa
Anirban Sen Chowdhary
 
How ring central sdk changing the game
How ring central sdk changing the gameHow ring central sdk changing the game
How ring central sdk changing the game
Anirban Sen Chowdhary
 
When ring central connect salesforce
When ring central connect salesforceWhen ring central connect salesforce
When ring central connect salesforce
Anirban Sen Chowdhary
 
Mule 4 connecting ring central
Mule 4 connecting ring centralMule 4 connecting ring central
Mule 4 connecting ring central
Anirban Sen Chowdhary
 
Ring central sdk
Ring central sdkRing central sdk
Ring central sdk
Anirban Sen Chowdhary
 
Ring central with okta
Ring central with oktaRing central with okta
Ring central with okta
Anirban Sen Chowdhary
 
Ring central connecting salesforce overview
Ring central connecting salesforce overviewRing central connecting salesforce overview
Ring central connecting salesforce overview
Anirban Sen Chowdhary
 
Ring central call logs overview (part 2)
Ring central call logs overview (part 2)Ring central call logs overview (part 2)
Ring central call logs overview (part 2)
Anirban Sen Chowdhary
 

More from Anirban Sen Chowdhary (20)

Change the game with Game changer
Change the game with Game changerChange the game with Game changer
Change the game with Game changer
 
Ring central desktop app overview
Ring central desktop app overviewRing central desktop app overview
Ring central desktop app overview
 
Overview in ringcentral digital line
Overview in ringcentral digital lineOverview in ringcentral digital line
Overview in ringcentral digital line
 
Some basics with ring central
Some basics with ring centralSome basics with ring central
Some basics with ring central
 
Ring central and python
Ring central and pythonRing central and python
Ring central and python
 
RingCentral application development overview
RingCentral application development overviewRingCentral application development overview
RingCentral application development overview
 
Cloze connect ringcentral
Cloze connect ringcentralCloze connect ringcentral
Cloze connect ringcentral
 
Overview on ring central errors part 4
Overview on ring central errors part 4Overview on ring central errors part 4
Overview on ring central errors part 4
 
Setting up your ring central sandbox in steps
Setting up your ring central sandbox in stepsSetting up your ring central sandbox in steps
Setting up your ring central sandbox in steps
 
Overview on ring central errors: part 2
Overview on ring central errors: part 2Overview on ring central errors: part 2
Overview on ring central errors: part 2
 
Overview on ring central errors
Overview on ring central errorsOverview on ring central errors
Overview on ring central errors
 
Call recording overview ring central
Call recording overview  ring centralCall recording overview  ring central
Call recording overview ring central
 
Ring central engaging with amazon alexa
Ring central engaging with amazon alexaRing central engaging with amazon alexa
Ring central engaging with amazon alexa
 
How ring central sdk changing the game
How ring central sdk changing the gameHow ring central sdk changing the game
How ring central sdk changing the game
 
When ring central connect salesforce
When ring central connect salesforceWhen ring central connect salesforce
When ring central connect salesforce
 
Mule 4 connecting ring central
Mule 4 connecting ring centralMule 4 connecting ring central
Mule 4 connecting ring central
 
Ring central sdk
Ring central sdkRing central sdk
Ring central sdk
 
Ring central with okta
Ring central with oktaRing central with okta
Ring central with okta
 
Ring central connecting salesforce overview
Ring central connecting salesforce overviewRing central connecting salesforce overview
Ring central connecting salesforce overview
 
Ring central call logs overview (part 2)
Ring central call logs overview (part 2)Ring central call logs overview (part 2)
Ring central call logs overview (part 2)
 

Converting with custom transformer part 2

  • 1. By Anirban Sen Chowdhary
  • 2.
  • 3.
  • 4. .
  • 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 JSON content from a location and put it into another location and the outbound file will contain it’s corresponding XML content. You can also see a custom transformer in the middle which is responsible for this JSON payload conversion to XML directly.
  • 7. Now, let’s check the code for this flow :- <flow name="CustomJSONtoXMLTransformer" doc:name="CustomJSONtoXMLTransformer"> <file:inbound-endpoint path="E:backuptestnewfolder" responseTimeout="10000" doc:name="File"> <file:filename-regex-filter pattern="jsonFile.txt" caseSensitive="false"/> </file:inbound-endpoint> <file:file-to-string-transformer doc:name="File to String"/> <custom-transformer class="CustomJSONtoXMLTransformer" doc:name="JsonToXml"/> <logger message="#[message.payload]" level="INFO" doc:name="Logger" /> <file:outbound-endpoint path="E:backuptest" outputPattern="xmlFile.txt" responseTimeout="10000" doc:name="File" /> </flow> As you can see the file jsonFile.txt contains JSON content that will be converted directly into XML by the custom-transformer into a file xmlFile.txt
  • 8. Now let’s check the JSON content of file jsonFile.txt :- So, the above is the JSON content we need to convert using our custom transformer into a corresponding XML
  • 9. So our custom transformer java class is the following :- public class CustomJSONtoXMLTransformer extends AbstractMessageTransformer implements DiscoverableTransformer { @Override public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { String jsonStr = (String) message.getPayload(); JSONObject jsonObj; try { jsonObj = new JSONObject(jsonStr); System.out.println(XML.toString(jsonObj)); //To append <?xml version="1.0"?> /* StringBuilder builder = new StringBuilder(); builder .append("<?xml version="1.0"?>") .append(XML.toString(jsonObj)); String xml_version= XML.toString(jsonObj); xml_version= builder.toString(); return xml_version;*/ //Append ends return XML.toString(jsonObj); } catch (JSONException 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 XML and has been dispatched to location E:backuptest with file name xmlFile.txt
  • 11. Now if we open the file xmlFile.txt from location E:backuptest we will get our XML content as following You can see you have generated the XML for the JSON directly just using a simple custom transformer component
  • 12. Hope you enjoyed the simple yet an amazing trick in Mule