Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Create a Java object to use in tJavaRow

My use case is as follows:

Read data from a JSON file via tFileInputJSON

Manipulate data inside tJavaRow

Output new data as JSON file via tFileOutputJSON

 

Inside the tJavaRow I need to use a custom object called Field. I am outputting a list of Fields based on data I am receiving from the tFileInputJSON component. A Field accepts an int and two Strings as parameters. An example field might be:

Field foo = new Field(1, "ID", input_row.ID);

 

I then need to propagate a List:

java.util.ArrayList<Field> fields = new java.util.ArrayList<Field>();

fields.add(foo);

 

And output it:

output_row.fields = fields;

 

How then can I create and use this Field object and output a List of them? I am aware of tJavaFlex as a way to pass code between components but have not had success with it either.

 

Thank you!

Labels (4)
1 Solution

Accepted Solutions
nfz11
Creator III
Creator III

The Field.java code would go in the Code/Routines folder in your project.  Then you can reference it in any tJava* component.  I would suggest you choose a name other than 'Field' that is less likely to clash with the name of classes in Talend's included libraries.  Select List as the type for your column and it should work as you show, though it may not work with generics so you might have to remove the types from your list definition.  If List doesn't work you can always select Object as the type for your column and cast it back to a list next time you use it.

View solution in original post

2 Replies
nfz11
Creator III
Creator III

The Field.java code would go in the Code/Routines folder in your project.  Then you can reference it in any tJava* component.  I would suggest you choose a name other than 'Field' that is less likely to clash with the name of classes in Talend's included libraries.  Select List as the type for your column and it should work as you show, though it may not work with generics so you might have to remove the types from your list definition.  If List doesn't work you can always select Object as the type for your column and cast it back to a list next time you use it.

Anonymous
Not applicable
Author

This works as expected, however when I go to create the outputJSON the output is simply a pointer to my Field object rather than the fleshed out object.

 

Result: { "fields": [routines.Field@123456, routines.Field@987654]}

 

Desired: {"fields":[

{

"fieldID": 1

"fieldName": "ID"

"fieldValue": "eg12345"

},

{

"fieldID": 2

"fieldName": "Date"

"fieldValue": "06/06/2019"

}]

 

Any insight as to how to achieve this desired output?

Thank you for your original solution