JsonDataSource for table fields get empty Jasper report

0

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

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-04-01T18:44:35 -->
<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="First" pageWidth="595" pageHeight="842" columnWidth="555"
              leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
              uuid="22badb1a-1eeb-43f9-84c2-0fcc00bc8ed1">
    <property name="com.jaspersoft.studio.data.defaultdataadapter"
              value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1"
                uuid="0fa82eee-1a87-497b-a0ea-900f60a11e5a">
        <property name="com.jaspersoft.studio.data.defaultdataadapter"
                  value="JssonAdapter"/>
        <parameter name="JSON_INPUT_STREAM" class="java.io.InputStream"
                   isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.source" class="java.lang.String"
                   isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.sources" class="java.util.List"
                   isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.date.pattern" class="java.lang.String"
                   isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.number.pattern" class="java.lang.String"
                   isForPrompting="false"/>
        <parameter name="JSON_LOCALE" class="java.util.Locale" isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.locale.code" class="java.lang.String"
                   isForPrompting="false"/>
        <parameter name="JSON_TIME_ZONE" class="java.util.TimeZone" isForPrompting="false"/>
        <parameter name="net.sf.jasperreports.json.timezone.id" class="java.lang.String"
                   isForPrompting="false"/>
        <queryString language="JSON">
            <![CDATA[data.marks]]>
        </queryString>
        <field name="subject" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="subject"/>
            <fieldDescription><![CDATA[subject]]></fieldDescription>
        </field>
        <field name="mark" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="mark"/>
            <fieldDescription><![CDATA[mark]]></fieldDescription>
        </field>
        <field name="grade" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="grade"/>
            <fieldDescription><![CDATA[grade]]></fieldDescription>
        </field>
        <field name="remarks" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="remarks"/>
            <fieldDescription><![CDATA[remarks]]></fieldDescription>
        </field>
        <field name="slNo" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="slNo"/>
            <fieldDescription><![CDATA[slNo]]></fieldDescription>
        </field>
    </subDataset>
    <parameter name="JSON_INPUT_STREAM" class="java.io.InputStream"
               isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.source" class="java.lang.String"
               isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.sources" class="java.util.List"
               isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.date.pattern" class="java.lang.String"
               isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.number.pattern" class="java.lang.String"
               isForPrompting="false"/>
    <parameter name="JSON_LOCALE" class="java.util.Locale" isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.locale.code" class="java.lang.String"
               isForPrompting="false"/>
    <parameter name="JSON_TIME_ZONE" class="java.util.TimeZone" isForPrompting="false"/>
    <parameter name="net.sf.jasperreports.json.timezone.id" class="java.lang.String"
               isForPrompting="false"/>
    <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[data.name]]></fieldDescription>
    </field>
    <field name="class" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="class"/>
        <fieldDescription><![CDATA[class]]></fieldDescription>
    </field>
    <field name="sub" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="sub"/>
        <fieldDescription><![CDATA[sub]]></fieldDescription>
    </field>
    <field name="interest" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="interest"/>
        <fieldDescription><![CDATA[interest]]></fieldDescription>
    </field>
    <field name="marks" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="marks"/>
        <fieldDescription><![CDATA[marks]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="33" y="7" width="48" height="21"
                               uuid="a959130a-4e63-4863-92e7-086469e6bf78"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <textField>
                <reportElement x="91" y="8" width="100" height="20"
                               uuid="7f1a710c-0489-47a7-9533-f41e8f6f4a43"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="34" y="46" width="48" height="21"
                               uuid="dc4b0314-d12a-4dc1-bff5-e9448046e941"/>
                <text><![CDATA[Class]]></text>
            </staticText>
            <textField>
                <reportElement x="92" y="47" width="100" height="20"
                               uuid="d7c7900b-5512-4467-80c8-9b0c66fbcc88"/>
                <textFieldExpression><![CDATA[$F{class}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="371" y="8" width="48" height="21"
                               uuid="559434cc-b422-41c8-8d8a-0b19d4ebd0e3"/>
                <text><![CDATA[Sub]]></text>
            </staticText>
            <textField>
                <reportElement x="429" y="9" width="100" height="20"
                               uuid="8574c427-44d2-41c7-9bc1-992ef440760a"/>
                <textFieldExpression><![CDATA[$F{sub}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="372" y="47" width="48" height="21"
                               uuid="44ed3736-f3a0-4152-8dc3-d7a5c4568c1a"/>
                <text><![CDATA[Interest]]></text>
            </staticText>
            <textField>
                <reportElement x="430" y="48" width="100" height="20"
                               uuid="0d0a5075-a40e-4a33-ae2d-df63b9af7836"/>
                <textFieldExpression><![CDATA[$F{interest}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="53" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="11" width="555" height="30"
                               uuid="2ea65437-85f8-46dd-aa7f-02b98dfb8bd9"/>
                <text><![CDATA[This is page header]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="11" width="555" height="30"
                               uuid="69fdcc8d-6c4b-42c2-ae35-65cd0c37212f"/>
                <text><![CDATA[This is column header]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="248" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="190"
                               uuid="850f53a4-bce5-4b02-a101-96d6d7568e73">
                    <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="Dataset1" uuid="12537d1c-a692-434e-99d8-a5f011036998">
                        <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_DATA_SOURCE}).subDataSource()]]>
                        </dataSourceExpression>
                    </datasetRun>
                    <jr:column width="70" uuid="c5dd80ce-11ef-47ed-9316-9b226175d9ce">
                        <property name="com.jaspersoft.studio.components.table.model.column.name"
                                  value="Column1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="70" height="30"
                                               uuid="6d2c73b7-78f6-4a5d-9479-913ff0474ff8"/>
                                <textFieldExpression>
                                    <![CDATA[$F{slNo}]]>
                                </textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="210" uuid="20986a7e-3c06-4f05-9da1-03d1f460a4dd">
                        <property name="com.jaspersoft.studio.components.table.model.column.name"
                                  value="Column2"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="210" height="30" uuid="a133b090-1158-4d32-9744-4643266d3134"/>
                                <textFieldExpression><![CDATA[$F{subject}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="f403ccaf-c666-45ed-a1c2-dc1d6c51ff96">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="70" height="30" uuid="b00164fa-1bb9-4167-b261-a848918bd880"/>
                                <textFieldExpression><![CDATA[$F{mark}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="fabb5be0-4e95-431a-894b-e060c05914dc">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="70" height="30" uuid="a1abb743-fd21-4bd6-b6ad-a7848afdcf03"/>
                                <textFieldExpression><![CDATA[$F{grade}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="130" uuid="f1f3d0ac-7be0-4ae6-beed-8cc4561762b7">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="130" height="30" uuid="24d015ce-4576-4d15-bcbd-02ab8be3a55f"/>
                                <textFieldExpression><![CDATA[$F{remarks}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="59" splitType="Stretch">
            <staticText>
                <reportElement x="350" y="11" width="105" height="19" uuid="4934aad9-ef6a-466a-8343-71d4fa462087"/>
                <text><![CDATA[Report created on]]></text>
            </staticText>
            <textField pattern="MMMMM dd, yyyy">
                <reportElement x="456" y="12" width="100" height="16" uuid="24e8a330-1496-4a92-9c4d-f41708e7dd2c"/>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

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"); 

My issue is Other fields are getting populated, but table values are getting as null. How I can fix this

johnykutty.mathew's picture
Joined: Mar 30 2017 - 12:14am
Last seen: 2 years 3 months ago
narcism - 2 years 4 months ago

Yes, I had posted same qstn in SO also.  I got answer from another question in SO, 

johnykutty.mathew - 2 years 4 months ago

0 Answers:

No answers yet
Feedback