Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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?
Hello,
If you are using the subscription version, perhaps you can take a look to the TalendDataMapper.
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?
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.
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.
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.