Although TDI 7.1 FP5 ships with a JSON parser, it would appear that the parser doesn’t actually work (even at FP6). The example AL that is shipped doesn’t work either – they both generate the same error – it looks like the JSON parser doesn’t quite register properly within the AL;
15:30:32,468 ERROR - [WriteJSON] CTGDIS810E handleException - cannot handle exception , initialize java.lang.Exception: CTGDIS159E This Connector has no configured Parser. at com.ibm.di.connector.Connector.initParser( Connector.java:567) at com.ibm.di.connector. FileConnector.openWriteFile( FileConnector.java:343)
This may be resolved in 7.1.1, but in this environment that version isn’t available.
Of course there is a workaround in the form of JSON Simple. Drop the JSON Simple json-simple-1.1.1.jar into the “<IDI install dir>\jars” folder & restart TDI to make it available.
JSON Encoding
This is a simple approach which encodes an object as a JSON string. The encoding will accept quite complex structures too. I have it quite happily accepting a java.util.LinkedList where each element is itself a HashMap. The only trick is that the JS object need to have been initialised properly;
// Init the object var theObject = org.json.simple.JSONValue.parse("{}"); // Populate the object theObject.name = "Mr Object"; theObject.tel = "999 ObjectHelp"; // Get the JSON string var jsonString = org.json.simple.JSONValue.toJSONString(theObject);
JSON decoding
To decode JSON – again use a FileSystem connector & the following code;
var jsonString = work.getString("jsonString"); if ((jsonString != "") && (jsonString != null)) { var jsonWrapper = org.json.simple.JSONValue.parse(jsonString); g_QueryResult = jsonWrapper[0]; }
Generating JSON obviously has many uses, but in this case I was generating data to be consumed by a couple of jquery plugins – DataTables and jqplot. More on how to generate the correct structure for those plugins later.