The resource attached to this project is an Eclipse Workspace with two projects. The first, called JSONParser is a Java class capable of manipulating JSON data such that it can be loaded into a BIRT scripted data set. The second project, called "Sample Reports", contains two BIRT reports with Scripted Data Sets bound to a JSON source.<br />
<br />
<br />
There are three steps to processing JSON data with this class:<br />
<br />
1) Create and empty report and add a hidden parameter called "JSONURL". Give this parameter the value of the URL where your JSON data can be found.<br />
<br />
2) Add a new Scripted Data Source to your report. You will need to modify the "open" script on the data source to something like the following (sample taken from BIRTJobs.rptdesign):
// This will track your current row later on
count = 0;
// Create instance of the Controller class
controller = new Packages.com.actuate.json.JSONParser();
// Load the JSON Source
controller.loadData(params["JSONURL"]);
// Calculate the total rows we will have
totalCount = controller.getChildCount("value/items");
<br />
The JSON parser supports an XPath-style query syntax so you can target nested elements. The synatx above points to the "items" element and gets a count of the objects under it. The purpose of this script block is to instantiate the JSON parser and to get a total count of objects under "items". This count is in fact the number of rows on our Data Set.<br />
<br />
<br />
3) Add a new Data Set and bind it to the Data Source you just created. Once the data set is created, you can add each of the columns you intend to populate from the JSON data. After creating these columns, modify the "fetch" script on the Data Set. This is the logic that will serve to populate the rows in the Data Set. The script will look something like this (sample taken from BIRTJobs.rptdesign):<br />
The scirpt read values off the JSON stream via the parser. In some cases the values are stored in child objects nested in the stream (location). XPath-style syntax is used to drill into those child objects. Because the process of populating the rows is somewhat asynchronous, we use the count value to drill directly to a location in the items array (inside the "getValue()" method). This allows us to traverse an array wiuthout the parser maintianing any sort of state with respect to the Report Layer.
if(count < totalCount){
row["URL"] = controller.getValue("value/items", "link", count);
row["title"] = controller.getValue("value/items", "title", count);
row["description"] = controller.getValue("value/items", "description", count);
row["type"] = controller.getValue("value/items", "g:job_type", count);
row["function"] = controller.getValue("value/items", "g:job_function", count);
// Location
row["city"] = controller.getValue("value/items", "g:location/city", count);
row["state"] = controller.getValue("value/items", "g:location/state", count);
row["zip"] = controller.getValue("value/items", "g:location/postal", count);
row["country"] = controller.getValue("value/items", "g:location/country", count);
count++;
return true;
}
return false;
<br />
The two sample reports are fully functional. They each consume live JSON data and render the data in a useful way via the BIRT toolset.<br />
<br />
BIRTJobs.rptdesign: The Data Source is bound to a custom Yahoo Pipe that searches for current job postings requiring BIRT. The data are fairly straightforward and provide a good starting point as to how to use the parser with other similar data sets.<br />
<br />
24hourTwitterTrends.rptdesign: This is a somewhat more complex data source because of its multi-dimensional nature. It requires a two-tiered loop to be iterated over in the scripting layers. For each day that is processed (7 total) the top trends for that day are additionally processed. As such, there is a more complex execution path in the "fetch" script for this report.<br />
<br />
<br />
The java package aslo features test classes to parse out JSON data to the command line. Feel free to start there in order to get a feel for how to set up your parser for success when you attempt to bind it to the BIRT Data Set.<br />
<br />
Good Luck!