I am trying to make a sticker using jasper report.
below image shows the structure.
I am passing a query to generate jasper report.
I have design the above structure in 'detail field' of jasper report so it can't repeat ,now I need to generate my query-result multiple times.
which means if currently my query gives me below result:-
I wish to produce the same result n times.
So If my query repeats the same row n times then it will automatically generate this structure 4 or n times
So anybody knows how to repeat the same row in mysql result query or anybody have better solution than this to do this job.
my expected result is as below
Modifying query is one solution example by using union
SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
but I will give you a pure jasper-reports solution, defining a parameter that indicates how many times your print should be repeated
The pure jasper reports solution is using a subreport, passing an
JREmptyDatasource(nrOfPrints) and the fields as parameters. The subreport will repeat the detail band as many time as the nrOfPrints and you can output the parameters (your main report fields) in it.
Example
Main report
The parameter that defines how many times to repeat is RepeatNumber also see how I pass the fields as parameter to the subreport, you need to pass all your fields.
<?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="RepatDataSource" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ceca1b98-d43c-4ee0-8339-661aa2ea53a9">
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>
<parameter name="RepeatNumber" class="java.lang.Integer">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[Your query]]>
</queryString>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<detail>
<band height="100" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="555" height="100" uuid="9d56da00-c1c9-4b2b-94e2-4019e4f58c8f"/>
<subreportParameter name="NR_REPEAT">
<subreportParameterExpression><![CDATA[$P{RepeatNumber}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="field1">
<subreportParameterExpression><![CDATA[$F{field1}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{RepeatNumber}.intValue())]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "RepatDataSource_subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
Subreport
<?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="RepatDataSource_subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="98ddead4-e116-4c91-9ecb-416c10c3065c">
<parameter name="NR_REPEAT" class="java.lang.Integer"/>
<parameter name="field1" class="java.lang.String" isForPrompting="false"/>
<detail>
<band height="108" splitType="Stretch">
<textField>
<reportElement x="328" y="1" width="100" height="20" uuid="c5642fd7-9f63-4aa5-8503-16b1388c156b"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT} + "/" +$P{NR_REPEAT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="45" y="35" width="125" height="20" uuid="5c2bb49a-ba95-4cb7-8c46-c32a0769e5e9"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$P{field1}]]></textFieldExpression>
</textField>
<line>
<reportElement x="0" y="0" width="555" height="1" uuid="dd3e7e6c-979e-421b-9f71-479e64c8023b"/>
</line>
<staticText>
<reportElement x="0" y="35" width="45" height="20" uuid="0443e2f4-25bd-4837-9c95-bca2b26b3996"/>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="214" y="1" width="114" height="20" uuid="0e20ec29-0092-41a3-b977-f8f64ff842ea"/>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Print Educational Books]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Output (with 1 record from my database)
Related
Let's assume we have this JSON:
{
"header" : "some header",
"strings": [
"foo",
"bar",
"baz"
]
}
And we want to display it in jasper report - header as a textfield, and the array in a list.
This is the report, which has access the array, but does not display all the elements of it - only first of them appears.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd -->
<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" pageWidth="595" pageHeight="100" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="6c570f43-054a-44a9-a9b1-ba4cf2cdcf86">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="json-sample"/>
<subDataset name="ListDataSet" uuid="fb812d2a-cb63-473c-b7d3-149977476dd0">
<field name="val" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="strings.*"/>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[]]>
</queryString>
<field name="header" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="header"/>
<fieldDescription><![CDATA[header]]></fieldDescription>
</field>
<field name="strings" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="strings"/>
<fieldDescription><![CDATA[strings]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<pageHeader>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="d98a55eb-ebbd-4cdd-8fa2-ae052b124aaa"/>
<textFieldExpression><![CDATA[$F{header}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="30" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="450" height="30" uuid="887dc258-2eac-4492-ae69-f9b2ffd1faee">
<property name="com.jaspersoft.studio.unit.CONTENTS.width" value="px"/>
</reportElement>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Horizontal">
<datasetRun subDataset="ListDataSet" uuid="1316cfd7-fe59-440f-a155-501914c00056">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)($P{REPORT_DATA_SOURCE})).subDataSource()]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="100">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="4ee2d6b3-c681-433f-a170-4bb31697de58"/>
<textFieldExpression><![CDATA[$F{val}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
This is how the result looks like:
What is wrong with the report?
Your fields could be updated a little:
$F{header} => java.lang.String
$F{strings} => java.util.List - this makes it possible to use this field as the datasource for a subcomponent.
Then when you pass in the Datasource, just pass in the list:
new JRBeanCollectionDatasource($F{strings})
or new JRBeanArrayDatasource($F{strings})
For you List data set I would just:
$F{_THIS} => java.lang.String - I'm not sure if val works well, but I know for sure that _THIS works (both 5.2 and 6.3)
I'll try to post the JRXML if you need it.
I want to export to CSV with the Header and Footer only showing once
The current output:
Name Address Hobby
AAA US XXXXX
BBB UK XXXXX
(Footer)
Name Address Hobby
CCC ID XXXXX
DDD CC XXXXX
(Footer)
Name Address Hobby
EEE SA XXXXX
FFF ZM XXXXX
(Footer)
The desired output:
Name Address Hobby
AAA US XXXXX
BBB UK XXXXX
CCC ID XXXXX
DDD CC XXXXX
EEE SA XXXXX
FFF ZM XXXXX
(Footer)
So how do I get the Header and footer to only show once ?
EDIT:
Footer looks like
Version : 1.0.0
AcademicProgramBusinessEntityCentreDoma Execution Time : 00:00:00.00
/NWU/StudentInformation/AcademicProgramDevelopment Build: v1.0.9 - Dev
You can use net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name}(see the http://jasperreports.sourceforge.net/config.reference.html page for details) property for excluding bands (Page Header and Page Footer in your case) for exporter.
The sample:
The input data, csv file (datasource):
AAA,US,XXXXX
BBB,UK,XXXXX
CCC,ID,XXXXX
DDD,CC,XXXXX
EEE,SA,XXXXX
FFF,ZM,XXXXX
The jrxml file:
<?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="exclude_band_for_csv" language="groovy" pageWidth="595" pageHeight="120" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0" uuid="daaa60dc-b91b-4e9b-bbc0-6189af985ef9">
<property name="net.sf.jasperreports.export.csv.exclude.origin.band.1" value="pageHeader"/>
<property name="net.sf.jasperreports.export.csv.exclude.origin.band.2" value="pageFooter"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="Name" class="java.lang.String"/>
<field name="Address" class="java.lang.String"/>
<field name="Hobby" class="java.lang.String"/>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement uuid="9da294e6-b5b7-489f-9469-7edb539315da" x="117" y="0" width="380" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14"/>
</textElement>
<text><![CDATA[Page Header]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="6a615d39-86f1-4a74-8ae7-4f8ca8e19afe" x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="df0929d2-34c0-4561-ab98-e6e5ce37fd11" x="100" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="675381d8-57b3-427f-88e4-ec4725ea3462" x="200" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Hobby}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="45" splitType="Stretch">
<staticText>
<reportElement uuid="9da294e6-b5b7-489f-9469-7edb539315da" x="127" y="10" width="380" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14"/>
</textElement>
<text><![CDATA[Page Footer]]></text>
</staticText>
</band>
</pageFooter>
</jasperReport>
The report's design in iReport:
The result of JRPdfExporter will be:
Both bands are present in PDF file
The result of JRCsvExporter will be (the output csv file):
AAA,US,XXXXX
BBB,UK,XXXXX
CCC,ID,XXXXX
DDD,CC,XXXXX
EEE,SA,XXXXX
FFF,ZM,XXXXX
Both bands are absent in CSV file.
As you can see I've excluded two bands (Page Header and Page Footer) only for JRCsvExporter.
For more details you can also see this post: JasperReports: hide textfield when not HTML view
to prevent column header repeating you can use following property:
net.sf.jasperreports.export.{format}.exclude.origin.keep.first.{suffix}.{arbitrary_name}
for example to avoid column header repeating in csv you write:
<property name="net.sf.jasperreports.export.csv.exclude.origin.keep.first.band.1" value="columnHeader"/>
https://community.jaspersoft.com/wiki/how-can-i-suppress-page-headers-and-footers-when-exporting-xls
I'm having issues accessing the array from my JSON data source. I'm using jasperreports 4.5.1.
My contact.json file:
{ "Person": { "FirstName": "John", "LastName": "Smith", "Contacts": [ { "Type":
"Cell", "Number": "555-555-5555" }, { "Type": "Home", "Number": "666-666-6666" }
] }
}
My contact.jrxml:
<?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="contact" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="2.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="Contactdataset">
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="Type" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="Type"/>
</field>
<field name="Number" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="Number"/>
</field>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="FirstName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="Person.FirstName"/>
</field>
<field name="LastName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="Person.LastName"/>
</field>
<detail>
<band height="300" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="80" height="30"/>
<textElement/>
<text><![CDATA[First Name:]]></text>
</staticText>
<staticText>
<reportElement x="0" y="30" width="80" height="30"/>
<textElement/>
<text><![CDATA[Last Name:]]></text>
</staticText>
<textField>
<reportElement x="80" y="0" width="200" height="30"/>
<textElement/>
<textFieldExpression><![CDATA[$F{FirstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="80" y="30" width="200" height="30"/>
<textElement/>
<textFieldExpression><![CDATA[$F{LastName}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="130" y="166" width="150" height="23"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="Contactdataset">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Person.Contacts")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="23" width="150">
<textField>
<reportElement x="27" y="0" width="100" height="17"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Type}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
<staticText>
<reportElement x="157" y="150" width="100" height="16"/>
<textElement/>
<text><![CDATA[Type]]></text>
</staticText>
</band>
</detail>
I also tried passing Contactdataset as :
<queryString language="json">
<![CDATA[Person.Contacts]]>
</queryString>
and passed that in the datasourceexpression like:
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Contactdataset")]]></dataSourceExpression>
What I am doing wrong? I'm getting the following error:
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text :
((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Person.Contacts")
In JasperReports 4.5.1 there was no subDataSource() method for the JsonDataSource. It was introduced in JasperReports 5.0.4.
As mentioned by #Narcis, the subDataSource() method for the JsonDataSource was introduced in JasperReports 5.0.4. and with this fact, you cannot use it in jasperreports 4.5.1. You should then try creating Main report and a Subreport. Add the field FirstName & LastName in the detail band of Main report and create a Subreport to iterate over the Contacts data. Add this Subreport on the detail band of Main report, select SubReport Poperties: "Use a datasource expression" and set below value for the datasource expression:
new net.sf.jasperreports.engine.data.JsonDataSource(new ByteArrayInputStream($F{Contacts}.toString().getBytes()),"")
In your Subreport, add Type & Number field and it would print the nested array accordingly, for every node of the main array.
This is similar to previous questions, but it has me stumped. I'm using Studio 6.5.1 CE version. So I have json data in a file that starts
{
"users": {
"itemCount": 30,
"items": [{
"id": 1,
"username": "user1",
...
My main json query is blank, with a single field defined: itemCount with expression "users.itemCount".
I have a Details band table and "UserDataset" with jsonql query "users.items". There are fields "id" with expression "id", "username" with expression "username", etc. Data preview shows the fields accurately.
When running the report I do see the single itemCount field populated. The problem is the table is missing. Apparently I cannot successfully tie the Datasource to the table. I have tried all sorts of expressions, and I thought something like this should work:
<datasetRun...>
...
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_CONNECTION}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
However, I can never get the table populated. What am I doing wrong? (I have similar report formats with jdbc sources and I have no problem with those tables.)
I can't find any combination of query/expression that works, so I am appending the jrxml file with style info stripped out for viewing.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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="Users" pageWidth="792" pageHeight="612" orientation="Landscape" columnWidth="752" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="61928541-d006-4374-9164-985c6c4116c9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Users.json"/>
<subDataset name="UserDataset" uuid="effd13cd-71de-4771-b706-d68b1042b564">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Users.json"/>
<queryString language="jsonql">
<![CDATA[users.items]]>
</queryString>
<field name="id" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="id"/>
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="username" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="username"/>
<fieldDescription><![CDATA[username]]></fieldDescription>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[]]>
</queryString>
<field name="itemCount" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="users.itemCount"/>
<fieldDescription><![CDATA[itemCount]]></fieldDescription>
</field>
<detail>
<band height="116" splitType="Stretch">
<componentElement>
<reportElement x="-9" y="50" width="770" height="62" uuid="7d7f19b0-9800-43ab-86a4-45846dae7775">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="UserDataset" uuid="fdfe69e2-eeac-4287-9e41-4c39610a79be">
<datasetParameter name="JSON_INPUT_STREAM">
<datasetParameterExpression><![CDATA[$P{JSON_INPUT_STREAM}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.source">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.source}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.sources">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.sources}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.date.pattern">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.date.pattern}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.number.pattern">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.number.pattern}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="JSON_LOCALE">
<datasetParameterExpression><![CDATA[$P{JSON_LOCALE}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.locale.code">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.locale.code}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="JSON_TIME_ZONE">
<datasetParameterExpression><![CDATA[$P{JSON_TIME_ZONE}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="net.sf.jasperreports.json.timezone.id">
<datasetParameterExpression><![CDATA[$P{net.sf.jasperreports.json.timezone.id}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_CONNECTION}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
<jr:column width="30" uuid="65fed6e9-ae42-4e04-b0b4-e2c8ef4a1b27">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="30" height="30" uuid="becae13f-4af9-4884-9b38-ab19347e9455"/>
<text><![CDATA[USER_ID]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="30" height="30" uuid="8c6b26dc-c436-4e68-acd5-89c33e09dac6"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="224b44e6-a9c2-4407-891d-623c7d6b33dc">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="3c961dd1-f991-4cde-9d22-224d4f3ae86c"/>
<text><![CDATA[USERNAME]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="3acbeee1-e7a4-4f45-b54c-b10c69bf7a95"/>
<textFieldExpression><![CDATA[$F{username}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
<textField>
<reportElement x="0" y="10" width="100" height="30" uuid="683b48a8-3f4d-4a3a-9ab5-df8255699b8b"/>
<textFieldExpression><![CDATA[$F{itemCount}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="39" splitType="Stretch">
<textField>
<reportElement x="326" y="6" width="100" height="30" uuid="94b2cbaa-c1f0-4934-a304-a0c4c84486a0"/>
<textFieldExpression><![CDATA["Page" +$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
Your subDataset should have no query, like this:
<subDataset name="UserDataset" uuid="effd13cd-71de-4771-b706-d68b1042b564">
<field name="id" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="id"/>
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="username" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="username"/>
<fieldDescription><![CDATA[username]]></fieldDescription>
</field>
</subDataset>
And your table's datasetRun should only use the correct expression, like this, with nothing else in it (you were casting the REPORT_CONNECTION to JsonDataSource):
<datasetRun subDataset="UserDataset" uuid="fdfe69e2-eeac-4287-9e41-4c39610a79be">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("users.items")]]></dataSourceExpression>
</datasetRun>
I have a JSON data with a following structure
[{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
},
{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
},
{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
}]
I have created separate reports for details1 and details2 arrays.
Also I have create a report for one element of the main table refferring to details1 and details2 reports as a subreports.
The problem is the main report is printing only the a1 and a2 from the first element and then prints all elements from all details1 and details2 tables from all elements.
My goal is to create a report that will print a1,a2, details1, details2 from first element of the main table, then the second one and so on. How can I achive that?
In other words, how can I iterate the same report template over the JSON array?
You don't have to create subreports in order to reach the nested arrays. A simpler solution is to make use of subDatasets.
In your case you need to create subDatasets for each details key in your JSON source:
<subDataset name="details1" uuid="4563e834-a9e5-43b5-9f0a-824948c73c73">
<field name="A1" class="java.lang.String">
<fieldDescription><![CDATA[a1]]></fieldDescription>
</field>
</subDataset>
<subDataset name="details2" uuid="f703cb76-2a4a-44f1-9a42-227e180038d2">
<field name="A2" class="java.lang.String">
<fieldDescription><![CDATA[a2]]></fieldDescription>
</field>
</subDataset>
Your main query has to be empty in order to iterate over each object in the main JSON source:
<queryString language="json">
<![CDATA[]]>
</queryString>
You then need to use a structure that knows how to handle a subDataset like a table or a list. I'm choosing a list here since it easier to work with. For the first subDataset you would then have:
<componentElement>
<reportElement x="90" y="40" width="333" height="20" uuid="c3237c70-6b2e-43e3-aa21-5092d8b91afc"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="details1" uuid="f5fdc6a3-736f-43ce-b549-cd7332d19eb8">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details1")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="333">
<textField>
<reportElement x="10" y="0" width="130" height="20" uuid="07e3ff2a-3832-4b06-9275-cb1ee8e51cfe"/>
<textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
For the second subDataset the list component is identical.
Here is a simple JRXML with the complete solution:
<?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="Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="81afe112-ee1b-4443-8d1c-cb6d9ab95dd8">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JsonArrayDataAdapter.xml"/>
<subDataset name="details1" uuid="4563e834-a9e5-43b5-9f0a-824948c73c73">
<field name="A1" class="java.lang.String">
<fieldDescription><![CDATA[a1]]></fieldDescription>
</field>
</subDataset>
<subDataset name="details2" uuid="f703cb76-2a4a-44f1-9a42-227e180038d2">
<field name="A2" class="java.lang.String">
<fieldDescription><![CDATA[a2]]></fieldDescription>
</field>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="A1" class="java.lang.String">
<fieldDescription><![CDATA[a1]]></fieldDescription>
</field>
<field name="A2" class="java.lang.String">
<fieldDescription><![CDATA[a2]]></fieldDescription>
</field>
<detail>
<band height="99" splitType="Stretch">
<textField>
<reportElement x="72" y="16" width="100" height="24" uuid="698866c8-7d26-4bc7-8727-b4a56d239a53"/>
<textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="190" y="16" width="100" height="24" uuid="e775c6c0-4058-4bc4-8c7a-d4d381fd6e66"/>
<textFieldExpression><![CDATA[$F{A2}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="90" y="40" width="333" height="20" uuid="c3237c70-6b2e-43e3-aa21-5092d8b91afc"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="details1" uuid="f5fdc6a3-736f-43ce-b549-cd7332d19eb8">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details1")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="333">
<textField>
<reportElement x="10" y="0" width="130" height="20" uuid="07e3ff2a-3832-4b06-9275-cb1ee8e51cfe"/>
<textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
<componentElement>
<reportElement x="90" y="60" width="333" height="20" uuid="38f3ac11-ad3e-464c-813a-46132f23783f"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="details2" uuid="833a13c3-e9b8-4f56-9f8f-279d32d403e8">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details2")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="333">
<textField>
<reportElement x="10" y="0" width="130" height="20" uuid="3d9fb513-bfc9-4d95-a3da-16b95cf15e7c"/>
<textFieldExpression><![CDATA[$F{A2}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>