Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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!

0683p000009Ls4Y.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? 

 

 

Labels (1)
  • Other

1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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();

0683p000009LrV0.png

View solution in original post

1 Reply
Anonymous
Not applicable
Author

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();

0683p000009LrV0.png