Create a Java object to use in tJavaRow

Highlighted
Four Stars

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!


Accepted Solutions
Nine Stars

Re: Create a Java object to use in tJavaRow

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.

--
Please give Kudos and mark topics as solved where appropriate.

View solution in original post


All Replies
Nine Stars

Re: Create a Java object to use in tJavaRow

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.

--
Please give Kudos and mark topics as solved where appropriate.

View solution in original post

Four Stars

Re: Create a Java object to use in tJavaRow

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

2019 GARTNER 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

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog