Jump to content
We've recently updated our Privacy Statement, available here ×

Passing JSON Stream as Datasource to Subreport


nils.stritzel

Recommended Posts

Hi,

I want to show the following some json data in Jasper Report. The json could come from file or an URL, therefore I want to be totally flexible on this point. I did not see the possibility to pass the whole URL into DATA Adapter, simply setting the net.sf.jasperreports.http.data.url parameter in the report did not seem to have any effect.

I came across the hint to put in a subreport, but that did not work for me either. I came up with code below, but seemingly passing in the stream has no effect. 

I tried with

<subreportParameter name="REPORT_DATA_SOURCE">                    <subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JsonDataSource( new java.io.ByteArrayInputStream( $P{jsonData}.getBytes("UTF-8") ), ".")]]></subreportParameterExpression>                </subreportParameter>[/code]

and with 
 

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JsonDataSource( new java.io.ByteArrayInputStream( $P{jsonData}.getBytes("UTF-8") ), ".")]]></dataSourceExpression>[/code]

but to no avail.

json:

{ "labelData": [ { "batchNo": "9888", "boxNo": 1, "barcodeNo": "BB9B1399C2595DABE0530101007FAA1F", "noOfCards": 1000 }, { "batchNo": "9888", "boxNo": 10, "barcodeNo": "BB9B1399CA295DABE0530101007FAA1F", "noOfCards": 999 }, { "batchNo": "9888", "boxNo": 2, "barcodeNo": "BB9B1399D1F95DABE0530101007FAA1F", "noOfCards": 1000 } ] }[/code]

Main report:

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  --><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="MULTI_LABEL_WRAPPER" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="19f14ced-fdf1-4aef-9aa2-08d390afab43">    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>    <parameter name="jsonData" class="java.lang.String" evaluationTime="Early">        <defaultValueExpression><![CDATA["{"labelData":[{"batchNo":"9888", "boxNo":1, "barcodeNo":"BB9B1399C2595DABE0530101007FAA1F", "noOfCards":1000},{"batchNo":"9888", "boxNo":10, "barcodeNo":"BB9B1399CA295DABE0530101007FAA1F", "noOfCards":999},{"batchNo":"9888", "boxNo":2, "barcodeNo":"BB9B1399D1F95DABE0530101007FAA1F", "noOfCards":1000}]}"]]></defaultValueExpression>    </parameter>    <parameter name="JSON_INPUT_STREAM" class="java.lang.String" isForPrompting="false">        <defaultValueExpression><![CDATA[new java.io.ByteArrayInputStream($P{jsonData}.getBytes("UTF8"))]]></defaultValueExpression>    </parameter>    <queryString>        <![CDATA[select 1 from dual]]>    </queryString>    <title>        <band height="802" splitType="Stretch">            <property name="com.jaspersoft.studio.unit.height" value="px"/>            <textField>                <reportElement x="70" y="40" width="430" height="210" uuid="2158d753-58d5-49a8-a808-e79598600604"/>                <textFieldExpression><![CDATA[$P{jsonData}]]></textFieldExpression>            </textField>            <subreport>                <reportElement x="93" y="367" width="200" height="200" uuid="7ea0bc6f-17dc-4da2-840d-a9b06a10afce"/>                <subreportParameter name="jsonData">                    <subreportParameterExpression><![CDATA[$P{jsonData}]]></subreportParameterExpression>                </subreportParameter>                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JsonDataSource( new java.io.ByteArrayInputStream( $P{jsonData}.getBytes("UTF-8") ), ".")]]></dataSourceExpression>                <subreportExpression><![CDATA["MULTI_LABEL_DETAILS.jasper"]]></subreportExpression>            </subreport>        </band>    </title></jasperReport>[/code]

Subreport:

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  --><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="MULTI_LABEL_DETAILS" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0b376d70-a9c5-4b88-a8cf-e7e83ae35bdb">    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JSON DATA"/>    <property name="com.jaspersoft.studio.report.description" value=""/>    <parameter name="jsonData" class="java.lang.String">        <defaultValueExpression><![CDATA["N/A"]]></defaultValueExpression>    </parameter>    <queryString language="JSON">        <![CDATA[labelData]]>    </queryString>    <field name="batchNo" class="java.lang.Integer">        <property name="net.sf.jasperreports.json.field.expression" value="batchNo"/>        <fieldDescription><![CDATA[batchNo]]></fieldDescription>    </field>    <field name="boxNo" class="java.lang.Integer">        <property name="net.sf.jasperreports.json.field.expression" value="boxNo"/>        <fieldDescription><![CDATA[boxNo]]></fieldDescription>    </field>    <field name="barcodeNo" class="java.lang.String">        <property name="net.sf.jasperreports.json.field.expression" value="barcodeNo"/>        <fieldDescription><![CDATA[barcodeNo]]></fieldDescription>    </field>    <field name="noOfCards" class="java.lang.Integer">        <property name="net.sf.jasperreports.json.field.expression" value="noOfCards"/>        <fieldDescription><![CDATA[noOfCards]]></fieldDescription>    </field>    <title>        <band height="56">            <textField>                <reportElement x="40" y="10" width="100" height="30" uuid="43578753-2cbd-4518-abfd-5620c9240046"/>                <textFieldExpression><![CDATA[$P{jsonData}]]></textFieldExpression>            </textField>            <staticText>                <reportElement x="184" y="26" width="100" height="30" uuid="9788ad8e-ab4c-44f7-b1da-b1bc64ebe5da"/>                <text><![CDATA[static Text]]></text>            </staticText>        </band>    </title>    <detail>        <band height="125" splitType="Stretch">            <textField>                <reportElement x="9" y="70" width="141" height="30" uuid="01ad50bb-d75d-48f4-b204-4bb2fd61129e">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b3561fcb-f0ec-49f8-9efc-3bf3895e2b65"/>                </reportElement>                <textFieldExpression><![CDATA[$F{batchNo}]]></textFieldExpression>            </textField>            <textField>                <reportElement x="150" y="70" width="138" height="30" uuid="2532d8f2-ed29-4f10-b54b-ca896e4ae408">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="648d8041-f83f-422b-9974-15075dd08065"/>                </reportElement>                <textFieldExpression><![CDATA[$F{boxNo}]]></textFieldExpression>            </textField>            <textField>                <reportElement x="288" y="70" width="138" height="30" uuid="3cac6a1b-594a-41f7-a207-45b1fa5b6119">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="23f27815-59a7-4477-bd53-5ad111e6290b"/>                </reportElement>                <textFieldExpression><![CDATA[$F{barcodeNo}]]></textFieldExpression>            </textField>            <textField>                <reportElement x="426" y="70" width="138" height="30" uuid="1eee0f1c-2467-4d3e-b0a2-ebc88e1de0f4">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="1537dc86-aa41-42fd-87cc-581322440232"/>                </reportElement>                <textFieldExpression><![CDATA[$F{noOfCards}]]></textFieldExpression>            </textField>        </band>    </detail></jasperReport>[/code]

Any hints are appreciated!

Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Solved it myself using  <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JsonDataSource( new java.io.ByteArrayInputStream( $P{jsonData}.getBytes("UTF-8") ), "labelData")]]></dataSourceExpression>

And subreportParameter jsonData is not needed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...