Jasperreports - how to get JSON to populate a subreport
Asked Answered
P

3

5

I am trying to get a table to populate with the same data in my main report using Jasper/iReport. How can I get the Json to carry over?

In Edit table datasource I've tried:

((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE})

new ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE})

$P{REPORT_DATA_SOURCE}

$P{REPORT_CONNECTION}

All of which compile, but none of which actually generate a table. Any ideas? There is no method like cloneDataSource() for JSON so what is the alternative?

My ultimate goal is simple: to pass in JSON data and use it to generate a chart, and then have a table displaying the same information. I have been working for days on this, Jasper is giving me a hard time. Please help!

Polliwog answered 18/2, 2013 at 16:36 Comment(0)
P
12

I was struggling with a similar problem when using sub reports. It seems that the JSON datasource gets consumed by the main report so you need to invoke subDataSource() to get a fresh datasource for your sub reports and maybe for tables, too - haven't tested that yet though...

Data Source Expression: ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()

Padova answered 22/5, 2014 at 10:8 Comment(4)
ah yes that was my problem. i haven't tried it yet will do so now.Peeples
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression> has me all fixed upPeeples
it is not working for me. I get an error net.sf.jasperreports.engine.data.JsonDataSourc‌​e cannot be resolved or is not a typeMaroney
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}.subDataSource()]]></dataSourceExpression> worked for meFop
E
4

If anyone is using the JasperReports new Book Reports feature (available in version 6+) and you want to use a JsonDataSource, this is the way to pass the json payload to the subreports:

<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
  <subreportParameter name="REPORT_DATA_SOURCE">
    <subreportParameterExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></subreportParameterExpression>
  </subreportParameter>
  <subreportExpression><![CDATA["PROFILES_cover.jasper"]]></subreportExpression>
</p:subreportPart>

You could also pass a subset of the json payload by providing node location as a parameter of the subDataSource(java.lang.String selectExpression) method. Examples:

.subDataSource("Northwind.Customers")

or

.subDataSource("Northwind.Customers(Country == USA)")
Eugenieeugenio answered 3/7, 2015 at 2:34 Comment(0)
C
0

Just add Data Source Expression in your main report:

<subreport>
    <reportElement x="-6" y="0" width="570" height="59" uuid="ac54a12b-8022-49a1-bc90-90cff664e3a1" />
    <dataSourceExpression>
        <![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("footer")]]>
    </dataSourceExpression>
    <subreportExpression>
        <![CDATA["footer.jrxml"]]>
    </subreportExpression>
</subreport>

Its working for me. Hope it will help.

Chemaram answered 17/4, 2018 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.