java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -
i having issues populating data in subreport in title band of main report. data populated correctly in subreport in detail band of main report. trying find wrong in json query. appreciated.
json sample data source (expenses.json)
{ "expenses": { "date": "8 sep 2016", "accounts": [ { "title": "xyz corp (111)", "accountname": "xyz corp", "accountnumber": "111", "transactions": [ { "date": "21 jun 2016", "name": "gas", "price": "17.50" }, { "date": "12 may 2016", "name": "shopping", "price": "111.99" }, { "date": "30 apr 2016", "name": "movies", "price": "7.50" } ] }, { "title": "abc corp (222)", "accountname": "abc corp", "accountnumber": "222", "transactions": [ { "date": "2 jun 2016", "name": "coffee", "price": "3.25" }, { "date": "21 may 2016", "name": "tea", "price": "1.90" }, { "date": "20 apr 2016", "name": "juice", "price": "4.50" } ] } ] } }
main report (main.jrxml)
<?xml version="1.0" encoding="utf-8"?> <!-- created jaspersoft studio version 6.3.0.final using jasperreports library version 6.3.0 --> <!-- 2016-09-08t14:41:09 --> <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="expenses_main" pagewidth="595" pageheight="842" columnwidth="555" leftmargin="20" rightmargin="20" topmargin="20" bottommargin="20" uuid="8cf0f1cb-ccad-41ba-aa8a-44abd597de54"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="expenses json data"/> <querystring language="json"> <![cdata[expenses.accounts]]> </querystring> <field name="title" class="java.lang.string"> <fielddescription><![cdata[title]]></fielddescription> </field> <field name="date" class="java.lang.string"> <fielddescription><![cdata[expenses.date]]></fielddescription> </field> <background> <band splittype="stretch"/> </background> <title> <band height="71" splittype="stretch"> <statictext> <reportelement x="2" y="0" width="201" height="16" uuid="2e947a01-8b6b-4169-beaa-6b2dac0d4141"/> <text><![cdata[expense report]]></text> </statictext> <subreport> <reportelement x="2" y="44" width="550" height="22" uuid="1d364987-9047-4660-bd58-8a2bcfc8493d"/> <datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource()]]></datasourceexpression> <subreportexpression><![cdata["/users/arul/jaspersoftworkspacev2/reports/sub1.jasper"]]></subreportexpression> </subreport> <textfield> <reportelement x="442" y="0" width="100" height="16" uuid="1cfbe11b-4f85-4bad-9280-d8712b80a65c"/> <textfieldexpression><![cdata[$f{date}]]></textfieldexpression> </textfield> <statictext> <reportelement x="410" y="1" width="31" height="15" uuid="ee159651-5d7b-4e0e-a52a-cb71338891cc"/> <text><![cdata[date: ]]></text> </statictext> </band> </title> <detail> <band height="54" splittype="stretch"> <textfield> <reportelement x="2" y="10" width="550" height="20" uuid="3b6e1d4b-2a58-4684-b334-396807db1169"/> <textfieldexpression><![cdata["transactions " + $f{title}]]></textfieldexpression> </textfield> <subreport> <reportelement x="2" y="30" width="545" height="20" uuid="10fde2aa-2b0b-4c1c-90d5-410f2fb9369b"/> <datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource("transactions")]]></datasourceexpression> <subreportexpression><![cdata["/users/arul/jaspersoftworkspacev2/reports/sub2.jasper"]]></subreportexpression> </subreport> </band> </detail> </jasperreport>
subreport1 (sub1.jrxml)
<?xml version="1.0" encoding="utf-8"?> <!-- created jaspersoft studio version 6.3.0.final using jasperreports library version 6.3.0 --> <!-- 2016-09-08t14:43: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="expenses_summary" pagewidth="555" pageheight="802" columnwidth="555" leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" uuid="d8ef4ad1-3a01-4394-a78c-17f9d95f98de"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="expenses json data"/> <querystring language="json"> <![cdata[expenses.accounts]]> </querystring> <field name="accountname" class="java.lang.string"> <fielddescription><![cdata[accountname]]></fielddescription> </field> <field name="accountnumber" class="java.lang.string"> <fielddescription><![cdata[accountnumber]]></fielddescription> </field> <background> <band splittype="stretch"/> </background> <title> <band height="21"> <statictext> <reportelement x="0" y="0" width="100" height="20" uuid="258b2395-7fd1-4df0-b000-e2bfd2620fab"/> <textelement> <font isbold="true"/> </textelement> <text><![cdata[account summary]]></text> </statictext> </band> </title> <columnheader> <band height="21" splittype="stretch"> <statictext> <reportelement mode="opaque" x="0" y="0" width="100" height="20" backcolor="#dbd7d7" uuid="b04ff630-de8e-4a9b-9ea0-d402d4734ae7"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <text><![cdata[account name]]></text> </statictext> <statictext> <reportelement mode="opaque" x="100" y="0" width="100" height="20" backcolor="#dbd7d7" uuid="6f0d406e-8d99-4364-888c-38bc6c0fa8c6"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <text><![cdata[account number]]></text> </statictext> </band> </columnheader> <detail> <band height="21" splittype="stretch"> <textfield> <reportelement x="0" y="0" width="100" height="20" uuid="c140795a-02a5-4334-9c5f-dc50e8a8ff6f"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textfieldexpression><![cdata[$f{accountname}]]></textfieldexpression> </textfield> <textfield> <reportelement x="100" y="0" width="100" height="20" uuid="ac7a5340-4c86-45d2-a932-b7b0e347a421"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textfieldexpression><![cdata[$f{accountnumber}]]></textfieldexpression> </textfield> </band> </detail> </jasperreport>
subreport2 (sub2.jrxml)
<?xml version="1.0" encoding="utf-8"?> <!-- created jaspersoft studio version 6.3.0.final using jasperreports library version 6.3.0 --> <!-- 2016-09-08t14:43:55 --> <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="expenses_subreport" pagewidth="555" pageheight="802" columnwidth="555" leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" uuid="2e39bfaf-e964-4b5d-847e-6517de5718d4"> <field name="date" class="java.lang.string"/> <field name="name" class="java.lang.string"/> <field name="price" class="java.lang.string"/> <background> <band splittype="stretch"/> </background> <columnheader> <band height="20" splittype="stretch"> <statictext> <reportelement mode="opaque" x="0" y="0" width="100" height="20" backcolor="#dbd7d7" uuid="8e073b78-91de-4819-886a-2527d5634067"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textelement> <font isbold="true"/> </textelement> <text><![cdata[date]]></text> </statictext> <statictext> <reportelement mode="opaque" x="100" y="0" width="100" height="20" backcolor="#dbd7d7" uuid="e7e7d858-05eb-4fda-9ae6-d998b4a51fd8"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textelement> <font isbold="true"/> </textelement> <text><![cdata[name]]></text> </statictext> <statictext> <reportelement mode="opaque" x="200" y="0" width="100" height="20" backcolor="#dbd7d7" uuid="fef220d6-4f85-4fd7-8017-5b64aff64666"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textelement> <font isbold="true"/> </textelement> <text><![cdata[price]]></text> </statictext> </band> </columnheader> <detail> <band height="20" splittype="stretch"> <textfield> <reportelement x="0" y="0" width="100" height="20" uuid="fd021427-d26c-4c9c-8c71-c9f136f11a66"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textfieldexpression><![cdata[$f{date}]]></textfieldexpression> </textfield> <textfield> <reportelement x="100" y="0" width="100" height="20" uuid="b6cd9815-ba45-49dd-be46-60a6a835d3fd"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textfieldexpression><![cdata[$f{name}]]></textfieldexpression> </textfield> <textfield> <reportelement x="200" y="0" width="100" height="20" uuid="cf3e397e-5546-4490-a88c-7211f124475e"/> <box> <toppen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <leftpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <bottompen linewidth="1.0" linestyle="solid" linecolor="#000000"/> <rightpen linewidth="1.0" linestyle="solid" linecolor="#000000"/> </box> <textfieldexpression><![cdata[$f{price}]]></textfieldexpression> </textfield> </band> </detail> </jasperreport>
pdf generating using jasper reports 6.3.0 java api.
import net.sf.jasperreports.engine.jasperexportmanager; import net.sf.jasperreports.engine.jasperfillmanager; import net.sf.jasperreports.engine.jasperprint; import net.sf.jasperreports.engine.jasperreport; import net.sf.jasperreports.engine.query.jsonqueryexecuterfactory; import net.sf.jasperreports.engine.util.jrloader; import java.io.fileinputstream; import java.util.hashmap; import java.util.map; public class jaspertest { public static void main(string[] args) throws exception { jasperreport jasperreport = (jasperreport) jrloader.loadobjectfromfile("/users/arul/jaspersoftworkspacev2/reports/main.jasper"); map<string, object> jasperparameters = new hashmap<string, object>(); jasperparameters.put(jsonqueryexecuterfactory.json_input_stream, new fileinputstream("/users/arul/jaspersoftworkspacev2/data/expenses.json")); jasperprint jasperprint = jasperfillmanager.fillreport(jasperreport, jasperparameters); jasperexportmanager.exportreporttopdffile(jasperprint, "report.pdf"); } }
your going deep in initial json query
<querystring language="json"> <![cdata[expenses.accounts]]> </querystring>
date
out of scope.
the solution stay on expenses scope
<querystring language="json"> <![cdata[expenses]]> </querystring>
the subreport 1 (sub1.jasper
) call subdatasource("accounts")
expression
<datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource("accounts")]]></datasourceexpression>
however have 1 detail band record (expenses), solve issue can introduce subreport.
instead of directly calling sub report 2 introduce subreport 1_2 (sub1_2.jasper
) iterate again on accounts subdatasource("accounts")
, display title , include subreport 2 (sub2.jasper
) datasource on transaction .subdatasource("transactions")
main report (main.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="expenses_main" pagewidth="595" pageheight="842" columnwidth="555" leftmargin="20" rightmargin="20" topmargin="20" bottommargin="20" uuid="8cf0f1cb-ccad-41ba-aa8a-44abd597de54"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="json"/> <querystring language="json"> <![cdata[expenses]]> </querystring> <field name="date" class="java.lang.string"> <fielddescription><![cdata[date]]></fielddescription> </field> <background> <band splittype="stretch"/> </background> <title> <band height="71" splittype="stretch"> <statictext> <reportelement x="2" y="0" width="201" height="16" uuid="2e947a01-8b6b-4169-beaa-6b2dac0d4141"/> <text><![cdata[expense report]]></text> </statictext> <subreport> <reportelement x="2" y="44" width="550" height="22" uuid="1d364987-9047-4660-bd58-8a2bcfc8493d"/> <datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource("accounts")]]></datasourceexpression> <subreportexpression><![cdata["sub1.jasper"]]></subreportexpression> </subreport> <textfield> <reportelement x="442" y="0" width="100" height="16" uuid="1cfbe11b-4f85-4bad-9280-d8712b80a65c"/> <textfieldexpression><![cdata[$f{date}]]></textfieldexpression> </textfield> <statictext> <reportelement x="410" y="1" width="31" height="15" uuid="ee159651-5d7b-4e0e-a52a-cb71338891cc"/> <text><![cdata[date: ]]></text> </statictext> </band> </title> <detail> <band height="36" splittype="stretch"> <subreport isusingcache="false"> <reportelement x="2" y="10" width="545" height="20" uuid="10fde2aa-2b0b-4c1c-90d5-410f2fb9369b"/> <datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource("accounts")]]></datasourceexpression> <subreportexpression><![cdata["sub1_2.jasper"]]></subreportexpression> </subreport> </band> </detail> </jasperreport>
sub report 1_2 (sub1_2.jasper
)
<?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="expenses_subreport" pagewidth="555" pageheight="802" columnwidth="555" leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" uuid="2e39bfaf-e964-4b5d-847e-6517de5718d4"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="json"/> <field name="title" class="java.lang.string"/> <background> <band splittype="stretch"/> </background> <detail> <band height="40" splittype="stretch"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <textfield> <reportelement x="2" y="1" width="550" height="20" uuid="934d546e-aeeb-4760-b100-cbefed73c420"/> <textfieldexpression><![cdata["transactions " + $f{title}]]></textfieldexpression> </textfield> <subreport isusingcache="false"> <reportelement x="5" y="16" width="545" height="20" uuid="1b8b0487-9dfa-4b82-bfb1-13009cb74ece"/> <datasourceexpression><![cdata[((net.sf.jasperreports.engine.data.jsondatasource)$p{report_data_source}).subdatasource("transactions")]]></datasourceexpression> <subreportexpression><![cdata["sub2.jasper"]]></subreportexpression> </subreport> </band> </detail> </jasperreport>
sub report 1 , 2 (sub1.jrxml
, sub2.jrxml
)
no changes
result
Hi,
ReplyDeleteI am jobless now. Please give me some work if you have.
You can pay me whatever you feel reasonable after completion of the task.
What I can do:
Data entry, processing and conversion (I have typing speed more than 60-word per minute)
SEO: link building using various platforms such as forums, blogs, social media, Q&A websites and more
I know some popular programming languages such as PHP, Python, HTML, CSS, AHK etc. but I am not confident to my programming skills.
I can communicate in English comfortably but I'm not a native speaker.
What I can't do:
I can't do complex calculation.
I can't do graphic design related tasks....
Thanks
Pawan
Email: admin@e07.net