ZetCode

JasperReports JSON

last modified December 16, 2021

JasperReports JSON tutorial shows how to work with JSON data in JasperReports library.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented, ready-to-print documents.

JsonDataSource is a data source for JSON data. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and for machines to parse and generate. The official Internet media type for JSON is application/json. The JSON filename extension is .json.

JasperReports JSON example

The following application reads JSON data from an online testing service and creates a report from it with JasperReports library.

report.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
   http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
              name="report" topMargin="20" bottomMargin="20">

    <field name="id" class="java.lang.Long"/>
    <field name="name"/>
    <field name="username"/>

    <detail>
        <band height="15">

            <textField>
                <reportElement x="0" y="0" width="50" height="15"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression class="java.lang.Long">
                    <![CDATA[$F{id}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="70" y="0" width="100" height="15" />

                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression>
                    <![CDATA[$F{name}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="190" y="0" width="100" height="15" />
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression>
                    <![CDATA[$F{username}]]>
                </textFieldExpression>
            </textField>

        </band>
    </detail>

</jasperReport>

This is the report template file. The template contains the detail band, in which each element is repeated for every record provided by the data source.

<field name="id" class="java.lang.Long"/>
<field name="name"/>
<field name="username"/>

We have three fields. These are automatically mapped to the JSON properties.

report.gvy
@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.17.0')

import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.data.JsonDataSource

def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)

def url = 'https://jsonplaceholder.typicode.com/users'
def is = new URL(url).openStream()
def ds = new JsonDataSource(is)

def params = [:]
def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)

JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")

In the example, we take data from a JsonDataSource.

def url = 'https://jsonplaceholder.typicode.com/users'
def is = new URL(url).openStream()
def ds = new JsonDataSource(is)

We create an input stream to the specified URL. The input stream is then passed to the JsonDataSource.

def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)

The created data source is passed to JasperFillManager.fillReport.

JasperReports JSON data adapter example

In the second example, we create a data adapter for JSON.

json-adapter.xml
<?xml version="1.0" encoding="UTF-8"?>
<jsonDataAdapter class="net.sf.jasperreports.data.json.JsonDataAdapterImpl">
    <name>json-adapter</name>
    <dataFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="httpDataLocation">
          <url>https://jsonplaceholder.typicode.com/users</url>
    </dataFile>

    <language>json</language>
    <useConnection>false</useConnection>
    <timeZone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" xsi:type="java:java.lang.String">Europe/Prague</timeZone>
    <locale xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" xsi:type="java:java.lang.String">en_US</locale>
    <selectExpression></selectExpression>

</jsonDataAdapter>

The data adapter for JSON is created with jsonDataAdapter.

report.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
   http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
              name="report" topMargin="20" bottomMargin="20">

    <property name="net.sf.jasperreports.data.adapter" value="json-adapter.xml"/>

    <field name="id" class="java.lang.Long"/>
    <field name="name"/>
    <field name="username"/>

    <detail>
        <band height="15">

            <textField>
                <reportElement x="0" y="0" width="50" height="15"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression class="java.lang.Long">
                    <![CDATA[$F{id}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="70" y="0" width="100" height="15" />

                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression>
                    <![CDATA[$F{name}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="190" y="0" width="100" height="15" />
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression>
                    <![CDATA[$F{username}]]>
                </textFieldExpression>
            </textField>

        </band>
    </detail>

</jasperReport>

This is the template file.

<property name="net.sf.jasperreports.data.adapter" value="json-adapter.xml"/>

The data adapter is set with the net.sf.jasperreports.data.adapter property.

report.gvy
package com.zetcode

@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.17.0')
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.13')

import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.JREmptyDataSource

def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)

def params = [:]
def jrPrint = JasperFillManager.fillReport(jrReport, params, 
    new JREmptyDataSource())

JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")

The Groovy file just generates the report. Since JasperReports internally uses the Apache HTTP client, we add the necessary dependency.

In this tutorial, we have created a PDF file report from JSON data. List all JasperReports tutorials.