jassper-reports - csv formatting - csv

I've got the following jasper file:
This file should create a csv file and it also is. Though every 14th record is malformed (see csv under the jasper file). Has anyone got any idea why?
<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="statisticsTBT" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6719b7f6-2c0e-4979-b55c-8db017b6df05">
<property name="ireport.zoom" value="3.797498335832415"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="id" class="java.lang.Long"/>
<field name="code" class="java.lang.String"/>
<field name="dateReception" class="java.util.Date"/>
<field name="dateInput" class="java.util.Date"/>
<field name="dateClosure" class="java.util.Date"/>
<field name="dateSigned" class="java.util.Date"/>
<field name="dateSignedDef" class="java.util.Date"/>
<field name="startBemiddeling" class="java.util.Date"/>
<field name="endBemiddeling" class="java.util.Date"/>
<field name="startRechtsgeding" class="java.util.Date"/>
<field name="endRechtsgeding" class="java.util.Date"/>
<field name="startHoofdbestuur" class="java.util.Date"/>
<field name="endHoofdbestuur" class="java.util.Date"/>
<field name="initialServiceDate" class="java.util.Date"/>
<field name="dossiersInTreatmentRecp" class="java.lang.String"/>
<field name="dossiersInTreatmentClose" class="java.lang.String"/>
<field name="dossierType" class="java.lang.String"/>
<field name="initialServiceCode" class="java.lang.String"/>
<field name="cadastralIncome" class="java.lang.Integer"/>
<field name="proposedCadastralIncome" class="java.lang.Integer"/>
<field name="cadastralIncomeDef" class="java.lang.Integer"/>
<field name="reasonInValidDef" class="java.lang.String"/>
<field name="fiscalYear" class="java.lang.Integer"/>
<detail>
<band height="20" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="0" width="15" height="20" uuid="8bc70924-17fc-4371-bf24-bdcb5bd1a082"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="15" y="0" width="15" height="20" uuid="5a00c878-5a07-4556-ac59-ca93488d1c1a"/>
<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="30" y="0" width="15" height="20" uuid="33a84a35-106c-4843-9ade-628ebccc122f"/>
<textFieldExpression><![CDATA[$F{dateReception}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="45" y="0" width="15" height="20" uuid="24e5e1fc-2a96-4c4e-8979-142912148617"/>
<textFieldExpression><![CDATA[$F{dateInput}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="60" y="0" width="15" height="20" uuid="aa9dfe9e-705e-48ba-8bc5-78681421f0e2"/>
<textFieldExpression><![CDATA[$F{dateClosure}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="75" y="0" width="15" height="20" uuid="50776a2e-ea89-469c-80f5-a09ed923edff"/>
<textFieldExpression><![CDATA[$F{dateSigned}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="90" y="0" width="15" height="20" uuid="2784ebd9-380b-4ed9-9d84-6ca6b8d94b4c"/>
<textFieldExpression><![CDATA[$F{dateSignedDef}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="105" y="0" width="15" height="20" uuid="033adaec-b48b-4f05-a62b-3b8624affc62"/>
<textFieldExpression><![CDATA[$F{startBemiddeling}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="120" y="0" width="15" height="20" uuid="7ba5da83-fbe9-4cc3-90b5-cb09e8eab5e4"/>
<textFieldExpression><![CDATA[$F{endBemiddeling}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="135" y="0" width="15" height="20" uuid="b78d39ea-ba72-44dd-8d8a-97d69f910489"/>
<textFieldExpression><![CDATA[$F{startRechtsgeding}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="150" y="0" width="15" height="20" uuid="fc174497-43b4-40f6-b800-8d463f583416"/>
<textFieldExpression><![CDATA[$F{endRechtsgeding}]]></textFieldExpression>
</textField>
</band>
</detail>
11: 727, null, 17/02/14 0:00, 17/02/14 0:00, null, null, null, null, null, null, null
12: 729, null, 17/02/14 0:00, 17/02/14 0:00, null, null, null, null, null, null, null
13: 730, null, 14/02/14 0:, 17/02/14 0:, null, null, null, null, null, null, null
14: , , 00, 00, , , , , , ,
notice how row 14 takes the 00 and 00 from row 13.. It's the exact same result on row 28, 42, ...

Related

Jaspersoft jsonql iterate over list to get next element

I am using jaspersoft studio 6.6.0 for my report, and i have json as a datasource for my report, in my report there is a field to display next port name based on condition of current port, but i am not able to display that, my json is as like this
"routingLocs": [
{
"callOrder": 1,
"locCode": "ZWTHJ",
"isCurrentLoc": "N"
},
{
"callOrder": 2,
"locCode": "TRYAR",
"isCurrentLoc": "Y"
},
{
"callOrder": 3,
"locCode": "AUABP",
"isCurrentLoc": "N"
},
{
"callOrder": 4,
"locCode": "RAJPI",
"isCurrentLoc": "N"
}
]
now i have to display locCode of object which is next to object that has isCurrentLoc = "Y", for this i have created one field with expression as
<![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
This expression is printing "TRYAR" instead of "AUABP"
My field definition in JRXML to display the value is
<field name="nextPort" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression">
<![CDATA[routingLocs..locCode(^.isCurrentLoc == "Y")]]>
</property>
<fieldDescription><![CDATA[nextPort]]></fieldDescription>
</field>
please help me to fix this expression
Thanks in advance for any kind of support
You cannot land on a sibling element with a jsonql query, but you can filter the JSON in a separate subDataset to achieve what you want.
Provided there is some order in your JSON data, like the ascending order of the callOrder, you can start creating your main dataset with your filtering query, something like:
<queryString language="jsonql">
<![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
</queryString>
Then create a subDataset that accepts a MAIN_CALL_ORDER parameter and that will use the same JSON data that your main does:
<subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
<queryString language="jsonql">
<![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
</queryString>
<field name="locCode" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
<fieldDescription><![CDATA[locCode]]></fieldDescription>
</field>
</subDataset>
Here, the query will produce the first item that has the callOrder greater than the one sent from the main dataset.
Then this dataset should be linked to a list that will be passed the parameter like so:
<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="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
<datasetParameter name="MAIN_CALL_ORDER">
<datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:listContents height="30" width="460">
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
<textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
A basic working JRXML would look like this:
<?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="e9573346-c7d0-4b7a-a838-d2ff5b7b9512">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<subDataset name="nextPortDataset" uuid="a8b8a64f-4c6b-4c28-91db-e10f28cef52c">
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
<parameter name="MAIN_CALL_ORDER" class="java.lang.Integer"/>
<queryString language="jsonql">
<![CDATA[routingLocs.*(callOrder > $P{MAIN_CALL_ORDER})[0]]]>
</queryString>
<field name="callOrder" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="callOrder"/>
<fieldDescription><![CDATA[callOrder]]></fieldDescription>
</field>
<field name="locCode" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="locCode"/>
<fieldDescription><![CDATA[locCode]]></fieldDescription>
</field>
<field name="isCurrentLoc" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="isCurrentLoc"/>
<fieldDescription><![CDATA[isCurrentLoc]]></fieldDescription>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[routingLocs.*(isCurrentLoc == "Y")]]>
</queryString>
<field name="mainCallOrder" class="java.lang.Integer">
<property name="net.sf.jasperreports.jsonql.field.expression" value="callOrder"/>
<fieldDescription><![CDATA[mainCallOrder]]></fieldDescription>
</field>
<field name="mainLocCode" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="locCode"/>
<fieldDescription><![CDATA[mainLocCode]]></fieldDescription>
</field>
<field name="mainIsCurrentLoc" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="isCurrentLoc"/>
<fieldDescription><![CDATA[mainIsCurrentLoc]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="32" splitType="Stretch">
<staticText>
<reportElement x="115" y="0" width="115" height="30" uuid="4ec73015-1c44-441c-a80e-bff448923a29">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<text><![CDATA[callOrder]]></text>
</staticText>
<staticText>
<reportElement x="230" y="0" width="115" height="30" uuid="018825e6-aa24-4a11-85a4-760fa6cf913c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<text><![CDATA[locCode]]></text>
</staticText>
<staticText>
<reportElement x="345" y="0" width="115" height="30" uuid="16e13486-0a34-4c65-b512-00a4678c65f7">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
</reportElement>
<text><![CDATA[isCurrentLoc]]></text>
</staticText>
<staticText>
<reportElement x="0" y="2" width="60" height="30" uuid="ad4e4685-2696-472c-a3b6-4a9961aeb6dc">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<text><![CDATA[Main Dataset]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="110" splitType="Stretch">
<textField>
<reportElement x="115" y="0" width="115" height="30" uuid="3bffc0f6-fb80-443a-b06d-ebbe2b669388">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="bff05bbe-0c73-4e55-adec-461fd0f2ebff"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainCallOrder}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="1267f406-8492-4dd1-859a-a37f24ac545a">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cc7977d7-a541-4d3b-81d8-97be3451e130"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainLocCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="345" y="0" width="115" height="30" uuid="522f3a23-a2b4-443d-9ca5-9abbd54a07df">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="82e0773c-3e7d-4372-a44c-93a4bfcfd6b0"/>
</reportElement>
<textFieldExpression><![CDATA[$F{mainIsCurrentLoc}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="0" y="80" width="560" height="30" uuid="859af22b-90ec-41fb-acf0-36d4acf2b90a"/>
<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="nextPortDataset" uuid="55c7173b-0b04-4d4f-bd80-48cfe1ef620c">
<datasetParameter name="MAIN_CALL_ORDER">
<datasetParameterExpression><![CDATA[$F{mainCallOrder}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:listContents height="30" width="560">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="6db9b791-aaac-45e9-af0f-34546254b9e9"/>
<text><![CDATA[Filtering List]]></text>
</staticText>
<textField>
<reportElement x="115" y="0" width="115" height="30" uuid="ee03e48b-38dd-4814-ad7d-2549810c0991">
<property name="com.jaspersoft.studio.unit.x" value="px"/>
</reportElement>
<textFieldExpression><![CDATA[$F{callOrder}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="230" y="0" width="115" height="30" uuid="213ab052-9cdc-419a-8d3d-45db3bf6ef90"/>
<textFieldExpression><![CDATA[$F{locCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="345" y="0" width="115" height="30" uuid="a8ea32ea-150f-4d1f-8945-18c2b287c157"/>
<textFieldExpression><![CDATA[$F{isCurrentLoc}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
and produce the following output:
This sample needs a file-based data adapter that is linked to both main dataset and subDataset with this property:
<property name="net.sf.jasperreports.data.adapter" value="./DataAdapter.xml"/>
If you don't have a file-based data adapter, you could create one and export it to file from the Repository Explorer inside Jaspersoft Studio.

How to color odd list items in nested list?

I got the following json
{
"data": [
{
"name": "apple",
"sorts": [
{ "name": "green", "number": "6", "comment": "green apples are nice" },
{ "name": "yellow", "number": "1" },
{ "name": "red", "number": "2" }
]
},
{
"name": "banana",
"sorts": [
]
},
{
"name": "pear",
"sorts": [
{ "name": "green", "number": "6", "comment": "green pears are sour" },
{ "name": "purple", "number": "0", "comment": "so far we haven't seen purple pears" }
]
}
]
}
This is result I want:
I created this with 2 nested list and passing the name to the nested list.
This works well, but now I want to color the odd list items. That would be the Yellow Apples and Green Pears. I found some example on how to do that on a simple list, but I have a nested list so that won't work.
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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<subDataset name="DatasetFruits">
<queryString language="JSON">
<![CDATA[data]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="sorts" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="sorts"/>
<fieldDescription><![CDATA[sorts]]></fieldDescription>
</field>
</subDataset>
<subDataset name="DatasetFruitsSort">
<parameter name="FRUIT_NAME" class="java.lang.String"/>
<queryString language="JSON">
<![CDATA[data.sorts]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="number" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="number"/>
<fieldDescription><![CDATA[number]]></fieldDescription>
</field>
<field name="comment" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="comment"/>
<fieldDescription><![CDATA[comment]]></fieldDescription>
</field>
</subDataset>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement x="-20" y="43" width="595" height="30"/>
<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">
<datasetRun subDataset="DatasetFruits">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("data")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="595">
<componentElement>
<reportElement x="0" y="0" width="595" height="30" isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[!$F{sorts}.equals("[]")]]></printWhenExpression>
</reportElement>
<jr:list>
<datasetRun subDataset="DatasetFruitsSort">
<datasetParameter name="FRUIT_NAME">
<datasetParameterExpression><![CDATA[$F{name}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("sorts")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="595">
<textField>
<reportElement x="0" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$P{FRUIT_NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="400" y="0" width="190" height="30"/>
<textFieldExpression><![CDATA[$F{comment}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
If you're using JasperReports 6.3.1 or newer, the simplest solution is to switch to JSONQL and use a single list instead of nested lists. You can read about JSONQL data sources here.
With JSONQL you can have a single list that iterates over all data.sorts.* nodes and get the fruit name via a field that goes up the tree. Then with a single list you can have a simple conditional style to color odd rows.
The whole thing would look something like this:
<?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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af456afc-4615-4564-8841-81929ce2447f">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Adapter"/>
<style name="listRow">
<conditionalStyle>
<conditionExpression><![CDATA[$V{REPORT_COUNT} % 2 == 0]]></conditionExpression>
<style mode="Opaque" backcolor="#FFFF00"/>
</conditionalStyle>
</style>
<subDataset name="DatasetFruitsSortAll" uuid="5961129e-70f0-4d98-8d4e-524f2bbb28f9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Adapter"/>
<queryString language="JSONQL">
<![CDATA[data.sorts]]>
</queryString>
<field name="fruitName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="^^.name"/>
<fieldDescription><![CDATA[fruitName]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="number" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="number"/>
<fieldDescription><![CDATA[number]]></fieldDescription>
</field>
<field name="comment" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="comment"/>
<fieldDescription><![CDATA[comment]]></fieldDescription>
</field>
</subDataset>
<queryString language="JSONQL">
<![CDATA[]]>
</queryString>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="595" height="30" isRemoveLineWhenBlank="true" uuid="c4206620-ee0d-4e50-9336-1e1be2723c6f"/>
<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="DatasetFruitsSortAll" uuid="b9df9d8f-7ada-47eb-84e6-4870547e7bd9">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("data.sorts.*")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="595">
<frame>
<reportElement style="listRow" x="0" y="0" width="590" height="30" uuid="3b3fba3d-e4c5-499c-a752-b6caa847a448"/>
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="bd3ac2b2-1803-4e0f-afa8-a0bdcc54e6fb"/>
<textFieldExpression><![CDATA[$F{fruitName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="100" height="30" uuid="be871dcb-61a0-49bd-b565-af8aa27bc7f4"/>
<textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="400" y="0" width="190" height="30" uuid="bea6dae7-bca2-4c0a-a0b5-443a859c20ac"/>
<textFieldExpression><![CDATA[$F{comment}]]></textFieldExpression>
</textField>
</frame>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
If for some reason JSONQL or a single list do not suit you, you can color odd rows with nested lists by having a running row number in the nested list. To do that you'd need to pass the value forth and back to/from the nested subdataset via a parameter and a return value.
This is how the whole report would look like. The nesting subdataset has a variable called subRowCount which keeps the row count of the previous nested subdataset runs. The variable is passed to the nesteed subdataset via a parameter; the subdataset adds it to the current row number and uses the result in the conditional style. Then the final row count is returned back into the subRowCount variable.
<?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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af456afc-4615-4564-8841-81929ce2447f">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Adapter"/>
<style name="listRow">
<conditionalStyle>
<conditionExpression><![CDATA[$V{runningRowCount} % 2 == 0]]></conditionExpression>
<style mode="Opaque" backcolor="#FFFF00"/>
</conditionalStyle>
</style>
<subDataset name="DatasetFruits" uuid="5824cf79-a97e-4cf1-954b-cc0c327c8405">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Adapter"/>
<queryString language="JSON">
<![CDATA[data]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="sorts" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="sorts"/>
<fieldDescription><![CDATA[sorts]]></fieldDescription>
</field>
<variable name="subRowCount" class="java.lang.Integer" calculation="System">
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
</subDataset>
<subDataset name="DatasetFruitsSort" uuid="5961129e-70f0-4d98-8d4e-524f2bbb28f9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Adapter"/>
<parameter name="FRUIT_NAME" class="java.lang.String"/>
<parameter name="prevRowCount" class="java.lang.Integer"/>
<queryString language="JSON">
<![CDATA[data.sorts]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="number" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="number"/>
<fieldDescription><![CDATA[number]]></fieldDescription>
</field>
<field name="comment" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="comment"/>
<fieldDescription><![CDATA[comment]]></fieldDescription>
</field>
<variable name="runningRowCount" class="java.lang.Integer">
<variableExpression><![CDATA[$P{prevRowCount} + $V{REPORT_COUNT}]]></variableExpression>
</variable>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement x="-20" y="43" width="595" height="30" uuid="da841d21-e910-4ce3-b415-1c68cade0981"/>
<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="DatasetFruits" uuid="4baf7682-130d-4b12-a973-8ab6084373de">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("data")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="595">
<componentElement>
<reportElement x="0" y="0" width="595" height="30" isRemoveLineWhenBlank="true" uuid="c4206620-ee0d-4e50-9336-1e1be2723c6f">
<printWhenExpression><![CDATA[!$F{sorts}.equals("[]")]]></printWhenExpression>
</reportElement>
<jr:list printOrder="Vertical">
<datasetRun subDataset="DatasetFruitsSort" uuid="b9df9d8f-7ada-47eb-84e6-4870547e7bd9">
<datasetParameter name="FRUIT_NAME">
<datasetParameterExpression><![CDATA[$F{name}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="prevRowCount">
<datasetParameterExpression><![CDATA[$V{subRowCount}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("sorts")]]></dataSourceExpression>
<returnValue fromVariable="runningRowCount" toVariable="subRowCount"/>
</datasetRun>
<jr:listContents height="30" width="595">
<frame>
<reportElement style="listRow" x="0" y="0" width="590" height="30" uuid="3b3fba3d-e4c5-499c-a752-b6caa847a448"/>
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="bd3ac2b2-1803-4e0f-afa8-a0bdcc54e6fb"/>
<textFieldExpression><![CDATA[$P{FRUIT_NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="100" height="30" uuid="be871dcb-61a0-49bd-b565-af8aa27bc7f4"/>
<textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="400" y="0" width="190" height="30" uuid="bea6dae7-bca2-4c0a-a0b5-443a859c20ac"/>
<textFieldExpression><![CDATA[$F{comment}]]></textFieldExpression>
</textField>
</frame>
</jr:listContents>
</jr:list>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>

JasperReports. Using value from parent json in child table

JasperReports generate null to table cell if this value is not defined in table scope, but defined globally.
At the top of the report I have defined variables:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
Then I pass they to the Jasper table (markup simplified without dropping sense):
<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="Dataset1" uuid="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="220" uuid="09e23518-5d7f-45d9-9a22-c4b537f0d83f">
<jr:detailCell style="Table 2_TD" height="18">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<textField isStretchWithOverflow="true">
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="220" height="18" uuid="4d0b54e7-584e-4cc9-86f2-59d72b600f1b"/>
<textElement verticalAlignment="Middle">
<font size="9"/>
<paragraph leftIndent="5"/>
</textElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
Example for input json:
[
{
"groupName" : "Group1",
"elements" : [
{
"elementName" : "el11",
"elementValue" : "evl1"
},
{
"elementName" : "el12",
"elementValue" : "evl2"
}
]
},
{
"groupName" : "Group2",
"elements" : [
{
"elementName" : "el21",
"elementValue" : "ev21"
},
{
"elementName" : "el22",
"elementValue" : "ev22"
}
]
}
]
As result I see values for elementName and elementValue, but for groupName I see null. I want to see something like that:
Group1 | el11 | ev11 |
Group1 | el12 | ev12 |
Group2 | el21 | ev21 |
Group2 | el22 | ev22 |
If you really must use a table element and you have it in the Detail band, you could pass the groupName as a parameter to the table dataset:
First, you would add the groupName field to the main dataset:
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="groupName" class="java.lang.String">
<fieldDescription><![CDATA[groupName]]></fieldDescription>
</field>
The subDataset would become:
<subDataset name="Dataset1" uuid="c145c0b0-641e-4a32-8e07-265189715ef9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="data\JSONDdapterNew.xml"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<parameter name="groupNameParam" class="java.lang.String"/>
<field name="elementName" class="java.lang.String">
<fieldDescription><![CDATA[elementName]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<fieldDescription><![CDATA[elementValue]]></fieldDescription>
</field>
</subDataset>
The table datasetRun would become:
<datasetRun subDataset="Dataset1" uuid="db1f69ee-c0db-4ece-aacb-a181465bdc79">
<datasetParameter name="groupNameParam">
<datasetParameterExpression><![CDATA[$F{groupName}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("elements")]]></dataSourceExpression>
</datasetRun>
And then your textFieldExpression would go from $F{groupName} to $P{groupNameParam}.
The easier solution, that does not rely on a table element, would involve switching to the newer JSONQL(stable in Jaspersoft Studio since v6.4.0) language that allows traversals up the JSON tree:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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_v3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c8f0a97-e66c-4103-8305-10e7cefe9ca2">
<queryString language="jsonql">
<![CDATA[elements.*]]>
</queryString>
<field name="groupName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="^^.groupName"/>
<fieldDescription><![CDATA[Group Name]]></fieldDescription>
</field>
<field name="elementName" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementName"/>
<fieldDescription><![CDATA[Element Name]]></fieldDescription>
</field>
<field name="elementValue" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="elementValue"/>
<fieldDescription><![CDATA[Element Value]]></fieldDescription>
</field>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="30" uuid="ef173e64-d7d6-4afd-abba-9ad32bf204b6">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<text><![CDATA[Group Name]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="30" uuid="cd0d90cb-2ced-4f52-becb-7e171c0e7824">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<text><![CDATA[Element Name]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="30" uuid="7bee32cf-b618-489d-bb81-014d410ba074">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<text><![CDATA[Element Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="30" uuid="6866d5da-c4a4-4ae3-af9f-19d29e3d28dc">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b7c69e0c-11ed-4881-93c1-dd83cefb5d4c"/>
</reportElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="30" uuid="87262f8a-1548-44a3-887e-e916aa551eab">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="cef61be1-0d90-4575-8b39-00b300c52204"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="30" uuid="7bf9a789-1f10-4986-abd7-aa7c57bd1b9c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9a5a388b-f4ea-48dd-9b4f-82a7c58b2c22"/>
</reportElement>
<textFieldExpression><![CDATA[$F{elementValue}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

How to iterate over JSON array to create repeating report in jasperreports

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>

How to get dynamic no of tables using json

I want to create Jasper Report with Dynamic no of tables.FOR EXAMPLE:
User 1 - 5 tables
User 2 - 3 tables
User 3 - 6 tables
I tried with list option but my tables are not visible and number of pages are getting increased.
<subDataset name="tableDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
<queryString language="json">
<![CDATA[tableData]]>
</queryString>
<field name="description" class="java.lang.String">
<fieldDescription><![CDATA[description]]></fieldDescription>
</field>
</subDataset>
<subDataset name="listDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
<queryString language="json">
<![CDATA[listData]]>
</queryString>
<field name="tableData" class="java.lang.String">
<fieldDescription><![CDATA[tableData]]></fieldDescription>
</field>
</subDataset>
<queryString language="json">
<![CDATA[mainDataSet]]>
</queryString>
<field name="listData" class="java.lang.String">
<fieldDescription><![CDATA[listData]]></fieldDescription>
</field>
<componentElement>
<reportElement x="-1" y="170" width="830" height="150" uuid="ab0272db-d740-42c9-94e5-238a2cc4a63e"/>
<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="listDataSet" uuid="f80306fb-80fc-405b-b90d-f9fe6b6ee2e1">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("listData")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="150" width="830">
<componentElement>
<reportElement positionType="Float" x="0" y="67" width="825" height="76" uuid="6a8e89c4-d4ea-4d8f-b932-8fbb24d00725"/>
<jr:table>
<datasetRun subDataset="tableDataSet" uuid="6ab7d349-34c8-4db7-bf3c-4e638ca36f5c">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
<jr:columnHeader style="colBotLeftLine" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[DESCRIPTION]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="footerBotLeftLine" height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Grand Total]]></text>
</staticText>
</jr:columnFooter>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="734834e8-5896-4355-9454-6b188e86bfff"/>
<textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>
Json Data.
{mainDataSet :
[
{CompanyName : "XYZ",
listData:
[
{tableId:1,
tableName:"Table One",
tableData:
[
{description:"Table One Row Data One "},
{description:"Table One Row Data Two"}
]
},
{tableId:2,
tableName:"Table Two",
tableData:
[
{description:"Table Two Row Data One "},
{description:"Table Two Row Data Two"}
]
}
]
}
]
}
mainDataSet is my Report Data Set, listData is the data for list and tableData is the one i want to display in my tables.
NOTE
If I remove the
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
and put
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
Then Two tables are created with null values.
The query to main report let's leave it blank (and keep the mainDataSet node)
<queryString language="json"><![CDATA[]]></queryString>
Define the CompanyName field as
<field name="CompanyName" class="java.lang.String">
<fieldDescription><![CDATA[mainDataSet[0].CompanyName]]></fieldDescription>
</field>
The dataSourceExpression to pass to the jr:list
<datasetRun subDataset="listDataSet" uuid="71276e30-7777-44ae-b6d9-2087a4c51ca3">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("mainDataSet.listData")]]></dataSourceExpression>
</datasetRun>
Hence point to the mainDataSet.listData
The dataSourceExpression to pass to the table (we are now in the listData)
<datasetRun subDataset="tableDataSet" uuid="5bca90cb-1473-4ff6-82fc-1da5ae4fb44c">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
</datasetRun>