tJavaRow to output JSON

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

 

 


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


All Replies
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

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

An API-First Approach to Modernizing Applications

Learn how to use an API-First Approach to Modernize your Applications

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read