<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic A Simple Way to Convert an XML String to JSON in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284672#M58313</link>
    <description>&lt;P&gt;While you can create JSON using the tWriteJSONField component, if you want to simply convert an XML String into JSON (with the same format), there is a much simpler way.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&lt;SPAN&gt;All you need to do is use the json-org.jar (which can be downloaded&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://www.docjar.com/jar_detail/json-org.jar.html" target="_blank" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;&lt;SPAN&gt;) and a Talend routine. I have included an example of a routine below.....&lt;/SPAN&gt;&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;DIV class="codebox"&gt; 
  &lt;PRE&gt;package routines;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;



public class XmlToJSON {


    public static String xmlToJson(String xml){
    	String json ="";
    	try {
            JSONObject xmlJSONObj = XML.toJSONObject(xml);
            json = xmlJSONObj.toString();
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
		return json;
    }
        
}&lt;/PRE&gt; 
 &lt;/DIV&gt; 
&lt;/DIV&gt; 
&lt;P&gt;Once you have created the routine, right click on the routine in the project tree and select "Edit Routine Libraries". Then point it towards the Jar downloaded above.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Then all you need to do is create your XML, convert it to a String (use a tConvertType), then use your routine method to convert the XML String to a JSON String.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Easy&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009MACn.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/154443iC5B8CACEF3D12C6A/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009MACn.png" alt="0683p000009MACn.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Sep 2018 21:34:39 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-09-13T21:34:39Z</dc:date>
    <item>
      <title>A Simple Way to Convert an XML String to JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284672#M58313</link>
      <description>&lt;P&gt;While you can create JSON using the tWriteJSONField component, if you want to simply convert an XML String into JSON (with the same format), there is a much simpler way.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&lt;SPAN&gt;All you need to do is use the json-org.jar (which can be downloaded&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://www.docjar.com/jar_detail/json-org.jar.html" target="_blank" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;&lt;SPAN&gt;) and a Talend routine. I have included an example of a routine below.....&lt;/SPAN&gt;&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;DIV class="codebox"&gt; 
  &lt;PRE&gt;package routines;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;



public class XmlToJSON {


    public static String xmlToJson(String xml){
    	String json ="";
    	try {
            JSONObject xmlJSONObj = XML.toJSONObject(xml);
            json = xmlJSONObj.toString();
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
		return json;
    }
        
}&lt;/PRE&gt; 
 &lt;/DIV&gt; 
&lt;/DIV&gt; 
&lt;P&gt;Once you have created the routine, right click on the routine in the project tree and select "Edit Routine Libraries". Then point it towards the Jar downloaded above.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Then all you need to do is create your XML, convert it to a String (use a tConvertType), then use your routine method to convert the XML String to a JSON String.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Easy&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009MACn.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/154443iC5B8CACEF3D12C6A/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009MACn.png" alt="0683p000009MACn.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 21:34:39 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284672#M58313</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-09-13T21:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: A Simple Way to Convert an XML String to JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284673#M58314</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Sixteen Stars,&lt;/P&gt;
&lt;P&gt;Thank you for tuto. I've been surching a little time for a code like this. First i've found user components named : tConvertXMLtoJSON or the tXMLtoJson. They do the same as your routine. There is only one common think with the components and your routine, the take the xml tree in an intil order but the don't reproduce the same order. Element order is not respected. I don't know why because there is no attributs to specify any order. It's a real problem for me because the transformed JSON has an order to respect because of the API REST need on order (the order i've created in the enter XML) . Do you have an idea about this, please ?&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;Marc.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 16:57:31 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284673#M58314</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-11-15T16:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: A Simple Way to Convert an XML String to JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284674#M58315</link>
      <description>&lt;P&gt;Take a look here:&amp;nbsp;&lt;A href="https://github.com/stleary/JSON-java/issues/383&amp;nbsp;" target="_blank" rel="nofollow noopener noreferrer"&gt;https://github.com/stleary/JSON-java/issues/383&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;....and here:&amp;nbsp;&lt;A href="https://github.com/stleary/JSON-java/wiki/FAQ#why-isnt-ordering-allowed-in-jsonobjects" target="_blank" rel="nofollow noopener noreferrer"&gt;https://github.com/stleary/JSON-java/wiki/FAQ#why-isnt-ordering-allowed-in-jsonobjects&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm sure this can be achieved, but it shouldn't be needed. There is a recommendation in the first link&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Nov 2018 16:12:44 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284674#M58315</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-11-16T16:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: A Simple Way to Convert an XML String to JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284675#M58316</link>
      <description>&lt;P&gt;If the API needs the elements to be in a specific order, then &lt;EM&gt;it isn't JSON&lt;/EM&gt;. It's a custom format that looks a lot like like JSON but has ordering semantics added. You can't expect a generic JSON component to support the requirements of a non-standard data format.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jan 2019 15:11:30 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/A-Simple-Way-to-Convert-an-XML-String-to-JSON/m-p/2284675#M58316</guid>
      <dc:creator>PhilHibbs</dc:creator>
      <dc:date>2019-01-08T15:11:30Z</dc:date>
    </item>
  </channel>
</rss>

