Using Data Adapters To Fill Reports With JSON Data From Remote URL

This example shows how to use a data adapter to retrieve JSON data from a remote URL and use it to fill a report. 

The TIBCO JasperReports® Library offers built-in support for loading report data from remote URLs through data adapters.

Data in JSON, XML, CSV or Excel format, can be loaded from local files or from remote URLs using data adapters and it can then be parsed into records using data format specific report queries (JSON QL, XPath) and/or report field mapping metadata.

For more details about how this sample report uses the JSONQL query language to process the incoming JSON data into data source records, please see this documentation.

The following example shows how to retrieve data about the current status of London Tube Network stations in JSON format, from a remote public URL, and use it to generate a report.

We first need to create a JSON data adapter file to point to this remote source of data and we are using a TIBCO Jaspersoft® Studio wizard to do so:

The tubestatus.jrxml report attached to this page can be open in Jaspersoft® Studio and previewed using the above created data adapter by selecting it in the data adapter combobox at the top left corner of the report designer preview tab as see below:

For deploying this report and the data adapter to an instance of JasperReports Server, we would need to first make sure the report references the data adapter, as the preview operation did not actually link the two together.

We are going to use a relative path to point from within the JRXML to the data adapter resource we are going to create in TIBCO JasperReports® Server. This is because the JRXML will be uploaded to a JasperReports® Server report unit, which acts itself like a subfolder containing its own resources and thus the JRXML would not be considered as being in the same folder as the data adapter resource. Use two dots to point to the data adapter resource as being in a parent repository folder as seen below:

Now let's upload the two files to JasperReports Server. We can do this using either the Web UI of the JasperReports Server instance, or, in this case, the Jaspersoft® Studio plugin.

After having configured the connection to our JasperReports Server instance from within JasperReports Server, we create a folder called tubestatus in the Public folder of the JasperReports Server repository.

Right click the newly created /Public/tubstatus folder and pick the New option to create a new data adapter file resource.

Make sure you use the name of the XML file as the name of your data adapter resource, because this is how we referenced it from the report in a step above.

Pick the tubestatusDataAdapter.xml file from the Jaspersoft® Studio project to have it uploaded to JasperReports Server and hit Finish button.

Let's create a report unit and upload the JRXML report template file as well.

We can give any name we want to the report unit and then pick the local JRXML file from the Jaspersoft Studio project in the workspace.

We don't need to specify any data source for this report unit as the data adapter property we set previously in the JRXML report template itself is enough to connect our report to the remote URL from where the JSON data will be read and then processed.

After the report unit has been created, we can use the JasperReports Server Web UI to navigate to the repository folder where we uploaded it and run it.