Six Stars

tJavaRow to output JSON

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)

 

Thanks!

__tJavaRow.png

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 < 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? 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Six Stars

Re: tJavaRow to output JSON

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. 

 

output_row.body = items.toString();

__resolved.png

1 REPLY
Six Stars

Re: tJavaRow to output JSON

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. 

 

output_row.body = items.toString();

__resolved.png