<?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 tJavaRow to output JSON in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/tJavaRow-to-output-JSON/m-p/2366585#M130056</link>
    <description>&lt;P&gt;How can I return JSON from my tJavaRow component? The code (shown below) shows that I'm creating the desired JSON, but I don't know how to pass it along as the output of the component. I see that I can use the Talend Document type as schema output, but the Java JSON type is not compatible. Is there a Talend type that is? Or is there a Type that I can cast to? My end goal is to be able to return the JSON to the client (via RESTResponse)&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Thanks!&lt;/P&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="__tJavaRow.png" style="width: 856px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009Ls4Y.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/137820i40D69A87B28D57A5/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009Ls4Y.png" alt="0683p000009Ls4Y.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;PRE&gt;InputStream is = (InputStream)globalMap.get("tFileFetch_1_INPUT_STREAM");
Reader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

JSONArray items = new JSONArray();
int KEEP_EMPTY_STRINGS = -1;
String[] headers = null;
String line;
String delimiter = "\\|";

// iterate over file contents
while ((line = br.readLine()) != null) {
	JSONObject item = new JSONObject();
	
	// gank the headers and move on
	if (headers == null) {
		headers = line.split(delimiter, KEEP_EMPTY_STRINGS);
		continue;
	}
	
	// build JSON
	String[] data = line.split(delimiter, KEEP_EMPTY_STRINGS);
	for(int i = 0; i &amp;lt; headers.length; i++) {
		item.put(headers[i], data[i].trim());
	}
	
	items.add(item);
}     

// How can I return the JSON to the output_row so that the next component can work with the JSON? &lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 08:57:04 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-11-16T08:57:04Z</dc:date>
    <item>
      <title>tJavaRow to output JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tJavaRow-to-output-JSON/m-p/2366585#M130056</link>
      <description>&lt;P&gt;How can I return JSON from my tJavaRow component? The code (shown below) shows that I'm creating the desired JSON, but I don't know how to pass it along as the output of the component. I see that I can use the Talend Document type as schema output, but the Java JSON type is not compatible. Is there a Talend type that is? Or is there a Type that I can cast to? My end goal is to be able to return the JSON to the client (via RESTResponse)&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Thanks!&lt;/P&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="__tJavaRow.png" style="width: 856px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009Ls4Y.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/137820i40D69A87B28D57A5/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009Ls4Y.png" alt="0683p000009Ls4Y.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;PRE&gt;InputStream is = (InputStream)globalMap.get("tFileFetch_1_INPUT_STREAM");
Reader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

JSONArray items = new JSONArray();
int KEEP_EMPTY_STRINGS = -1;
String[] headers = null;
String line;
String delimiter = "\\|";

// iterate over file contents
while ((line = br.readLine()) != null) {
	JSONObject item = new JSONObject();
	
	// gank the headers and move on
	if (headers == null) {
		headers = line.split(delimiter, KEEP_EMPTY_STRINGS);
		continue;
	}
	
	// build JSON
	String[] data = line.split(delimiter, KEEP_EMPTY_STRINGS);
	for(int i = 0; i &amp;lt; headers.length; i++) {
		item.put(headers[i], data[i].trim());
	}
	
	items.add(item);
}     

// How can I return the JSON to the output_row so that the next component can work with the JSON? &lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 08:57:04 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tJavaRow-to-output-JSON/m-p/2366585#M130056</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-16T08:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: tJavaRow to output JSON</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tJavaRow-to-output-JSON/m-p/2366586#M130057</link>
      <description>&lt;P&gt;I was able to resolve this by simply setting the tJavaRow components output schema to string and using toString() on the java JSON object. I then passed the string output directly to the RESTResponse which in turn responds with JSON.&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;PRE&gt;output_row.body = items.toString();&lt;/PRE&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="__resolved.png" style="width: 999px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009LrV0.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/131051i427CC3D7F07CF4CF/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009LrV0.png" alt="0683p000009LrV0.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2017 17:55:03 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tJavaRow-to-output-JSON/m-p/2366586#M130057</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-12-14T17:55:03Z</dc:date>
    </item>
  </channel>
</rss>

