Any good tWriteJSONField tutorials?

Seven Stars

Any good tWriteJSONField tutorials?

I'm struggling with anything more than a very simple JSON structure. I can create a JSON that has one array element in it, but as soon as it needs anything more (e.g. an array of questions, each one of which contains an array of answers) I get lost. Are there any good examples of building complex JSON structures with tWriteJSONField components?

Tags (1)
Eight Stars

Re: Any good tWriteJSONField tutorials?

@PhilHibbs, I struggle all the time with this component. Infact, I too just posted another q with some additional specifics I am trying to achieve.
I hope some experts can help us here!
Thanks.
Seven Stars

Re: Any good tWriteJSONField tutorials?

I'd echo that. Can't seem to create a JSON and call the application APIs. This component is probably super broken. None of my questions on the forum ever got answered.
Six Stars

Re: Any good tWriteJSONField tutorials?

Hello,

 

If you are using the subscription version, perhaps you can take a look to the TalendDataMapper.

 

 

Seven Stars

Re: Any good tWriteJSONField tutorials?

Ok here's my quick notes after some experimentation.

 

1. For each array in a JSON object, you need a tWriteJSONField component.

2. To build the array:

2a. Add elements for the aggregation key, and set the aggregation key in the Component properties

2b. Add a named element to hold the array, give it a class attribute of array

2c. Add an element whose name is not important, give it a class attribute of object

2d. Set the unnamed object element to be the loop element

3. Follow up immediately with a tExtractJSONField component with a Loop Jsonpath query of "$"

4. Map the aggregation key element(s) and the array element into output fields

Now you have the key fields, and the array in a string. You can then join this flow to other flows for other sets of data, and further build up your JSON in more tWriteJSONField components.

The one peculiar thing that baffles me is, the array that you get out from the tExtractJSONField is a string. Normally, when you map a string into a JSON element, it wraps it in quotes. So if the Name column contains Fred, you get this:

 

"Name":"Fred"

However, when you map the constructed array to a JSON element, you get something like this, which is exactly what is wanted:

"Children":[{"Name":"Alice","Age":7},{"Name":"Bob","Age":5},{"Name":"Carole","Age":1}]

What I expected to see was this:

"Children":"[{\"Name\":\"Alice\",\"Age\":7},{\"Name\":\"Bob\",\"Age\":5},{\"Name\":\"Carole\",\"Age\":1}]"

Can anyone explain why it is doing what I actually want, rather than what I expected it to do?

Highlighted
Eight Stars

Re: Any good tWriteJSONField tutorials?

Yeah, Talend is supposed to be user-friendly but trust me, do yourself a favor and use an org.json.JSONObject instead. Now that I think about all the time I spent figuring out how to this very simple thing in Talend without real documentation (because you know, they need to work more on that), I could have developed a very sophisticated solution using pure java.

Nine Stars

Re: Any good tWriteJSONField tutorials?

I'd echo navds comments.

 

Anything more than a simple/flat JSON structure, construct it yourself, making copious use of tHashInput/Output components to store the constructed JSON as an object and tAggregateRow in LIST(Object) method.

 

Regards David
Dont forget to give Kudos when an answer is helpful or mark the answer as the solution.
Eight Stars

Re: Any good tWriteJSONField tutorials?

Thanks.Could you share a quick example of using org.json.JSONObject with this type of setup? I am not very savvy with using custom java code in tflex/ tjavarow. A quick rundown on how to work through the flows to make the json object will be helpful please.

 

Thank you.

Eight Stars

Re: Any good tWriteJSONField tutorials?

wow, thats neat. But, for me, I just discovered that tWriteJsonField would create an array instead of an object when single row is returned... This is baffling.
Eight Stars

Re: Any good tWriteJSONField tutorials?

Would you be able to share some examples of how to work with this? I am currently in the process of overhauling our web services served via ESB, and would love to learn how to create objects using pure java in Talend. A simple rundown with screenshots or something is appreciated. Thanks.
Eight Stars

Re: Any good tWriteJSONField tutorials?

Maybe someone has a better technique but I don't like to use dozens of tLibraryLoad to import external jars. I prefer adding it in routine dependencies. In this basic example, I downloaded manually the jar json-20171018.jar (yeah, I wish this could be done project-wide using maven), created a new routine 'Demo', edit routine libraries to add the jar. Now I have a JSON parser which could be used project-wise. Here, I returned a java.util.Map which should be familiar. 

Screenshot from 2019-01-17 16-53-27.pngImport External Library _003.pngbrowse on your computer


demo_routine_json.png

I hope this helps. 

 

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

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Why Companies Move to the Cloud: 7 Success Stories

Learn how and why companies are moving to the Cloud

Read Now