It is fine when create the Jasper report(PDF, Excel, Csv) by using JRBeanCollectionDataSource. It means that the .jrxml file accepts the collection of pojo as a input to process the report.
Now, I have been trying to create the jasper report with the same .jrxml but from JSON Object. I tried the following, but all values are null in pdf report
Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:reports/project.jrxml");
JsonDataSource ds = new JsonDataSource(new File("c:\myjson.json"));
jasperDesign = JRXmlLoader.load(resource.getInputStream());
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
JasperExportManager.exportReportToPdfFile(jasperPrint, destination+fileName+".pdf");
Can any one help me?
jrxml is a human readable XML file that contains the report template i.e. report structure and its formatting rules. . jasper is the compiled report template i.e. compiled . jrxml file.
Built on an open architecture, Jaspersoft reporting software lets you connect to any data source, deploy using any method, and work with modern RESTful APIs. Take Jaspersoft source code and add your own custom features to it.
I've just been struggling with using JSON as the DataSource to a Jasper Report and with a lack of decent examples on the Net I thought I would post this here for future reference.
This example is how to use iReport Designer and a JSON DataSource.
Firstly, the input JSON:
{
"userName": "Evil Raat",
"details": {
"email": "not_really@test.com"
}
}
Then create a JSON DataSource in iReport Designer and point it at your file (leaving all the other details as their defaults)
Next you can use the following jrxml template to render the above JSON into a report:
<?xml version="1.0" encoding="UTF-8"?>
<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="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a894078a-929b-4aae-a1d0-46485f0f8835">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[userName]]></fieldDescription>
</field>
<field name="userEmail" class="java.lang.String">
<fieldDescription><![CDATA[details.email]]></fieldDescription>
</field>
<title>
<band height="200" splitType="Stretch">
<textField>
<reportElement uuid="3b74775b-4555-43c3-bdf2-1677145c8660" x="0" y="31" width="555" height="20"/>
<textElement textAlignment="Right">
<font fontName="Helvetica" size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{userName}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="aa6cc7c8-2ca1-4f0f-92e2-c466083daba0" x="0" y="54" width="555" height="20"/>
<textElement textAlignment="Right">
<font fontName="Helvetica" size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{userEmail}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
NOTE: You must define the field elements first, before any they can be used. They should be the JSON path from the root of the JSON input file using standard dot notation. See the fieldDescription elements above for examples.
Once your field is defined you can use its computed value in a text field or whatever:
Hope that helps some people.
This is how I handle json arrays in jasper
Lets say I want to report off the following array.
[
{"name":"Jerry", "value":"Jesus"},
{"name":"Gideon", "value": "Loves"},
{"name":"Eva", "value": "You"}
]
When designing the report, make sure you name the fields the exact same name as your json field name. So in the designer I would add two fields called name and value. You can even add as many parameters to the report designer as needed. For this example, I will add a parameter called title in Jasper Studio.
Now here is the java code that will create the jasper report based off of this test array. I will hard code the json data in the code, but you can load from file or whatever you feel is best. I commented the code to explain what is happening.
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.data.JsonDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import org.apache.commons.codec.binary.Base64;
import java.util.HashMap;
import java.util.Locale;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.*;
//Class Name. This must match the class name you put in your build.gradle file
public class JasperPDFExample {
public static void main(String[] args) {
try {
try {
//Our json object. This can be loaded from file
String rawJsonData = "[{\"name\":\"Jerry\", \"value\":\"Jesus\"},"
+ "{\"name\":\"Gideon\", \"value\": \"Loves\"},"
+ "{\"name\":\"Eva\", \"value\": \"You\"}"
+ "]";
//Load compiled jasper report that we created on first section.
JasperReport report = (JasperReport) JRLoader.loadObject(new File("/home/jerry/Sample.jasper"));
//Convert json string to byte array.
ByteArrayInputStream jsonDataStream = new ByteArrayInputStream(rawJsonData.getBytes());
//Create json datasource from json stream
JsonDataSource ds = new JsonDataSource(jsonDataStream);
//Create HashMap to add report parameters
Map parameters = new HashMap();
//Add title parameter. Make sure the key is same name as what you named the parameter in jasper report.
parameters.put("title", "Jasper PDF Example");
//Create Jasper Print object passing report, parameter json data source.
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, ds);
//Export and save pdf to file
JasperExportManager.exportReportToPdfFile(jasperPrint,"/home/jerry/jasperpdfexample.pdf");
} catch (JRException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
Thanks to https://mis.io/pub/how-to-create-a-jasper-pdf-report-from-a-json-datasource-in-java/ I was able to get this to work along with setting up java for jasper using gradle build tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With