I am new to jasper reports.
I am trying to generate a pdf from a son file, which contains some info like name
, class
etc and an array marks
. I am trying to render the marks array as a table. my JSON is
{"data": {"name" : "Johny",
"class" : "A2",
"sub" : "cs",
"interest" : "films",
"marks" : [{
"subject" : "Maths",
"mark" : "24",
"grade" : "A",
"remarks" : "",
"slNo" : "1"
},
{
"subject" : "English",
"mark" : "24",
"grade" : "A",
"remarks" : "",
"slNo" : "2"
},
]
}}
I have given subdatasource expression as
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression>
Full jrxml - Here
Now I call these from java like,
File jsonFile = new File("/Volumes/Johny/Work/EclipseWorkspace/SecondReport/res/Marks.Json");
JasperDesign jasperDesign = JRXmlLoader.load(new File("/Volumes/Johny/Work/EclipseWorkspace/SecondReport/res/First.jrxml"));
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
parameters.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(jsonFile));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
JasperExportManager.exportReportToPdfFile(jasperPrint, "/Users/johnykutty/Desktop/Sample2.pdf");
Other fields are getting populated, but table values are getting as null.
Found answer from here
The issue was I have to pass the key for the array in dataSourceExpression
. So, the dataSourceExpression
should be like
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("marks")]]></dataSourceExpression>
Note the "marks" parameter to subDataSource()
method