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

Iterating json array on subreport


teemu.alander

Recommended Posts

Hi,

I searched already existing answers and got few tips what to do and also looked at this http://jasperreports.sourceforge.net/sample.reference/jsondatasource/ but couldn't iterate my jsonarray. Also this might quite basic question.

My Json looks like this:

{
  "customer": "test",
  "mappedExpenses": [
    {
      "uId": "lfpq3f0vfeqm2t9",
      "currencyId": "EUR",
      "type": "one",
    },
    {
      "uId": "plslhkxshfrpmn29",
      "currencyId": "EUR",
      "type": "two",
    }
  ]
}
 
And my jrxml files look like this 
 
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="Tapaturma/Ammattitauti-Ilmoitus" pageWidth="595" pageHeight="842" columnWidth="545" leftMargin="30" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4aab7094-0c60-44e4-969e-7571942136a8">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="sub" class="net.sf.jasperreports.engine.JasperReport"/>
<queryString language="json">
<![CDATA[mappedExpenses]]>
</queryString>
<field name="type" class="java.lang.String">
<fieldDescription><![CDATA[type]]></fieldDescription>
</field>
<detail>
<band height="196">
<subreport>
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="82" width="545" height="41" uuid="55732254-51f9-4434-aa69-c750a822aafc"/>
<subreportParameter name="type">
<subreportParameterExpression><![CDATA[$F{type}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub}]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
 
 
sub.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="tyonantajaSubreport" pageWidth="595" pageHeight="822" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="0c7de36a-8d80-48f1-9866-a4c25af4ae34">
<property name="ireport.zoom" value="2.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="json">
<![CDATA[mappedExpenses]]>
</queryString>
<field name="typeFromZero" class="java.lang.String">
<fieldDescription><![CDATA[mappedExpenses[0].type]]></fieldDescription>
</field>
<field name="mappedExpenses" class="java.lang.Object"/>
<field name="type" class="java.lang.String">
<fieldDescription><![CDATA[type]]></fieldDescription>
</field>
<background>
<band/>
</background>
<detail>
<band height="214">
<textField isBlankWhenNull="true">
<reportElement x="25" y="21" width="280" height="11" uuid="c4435536-5e25-497f-ac4c-123ef740955f"/>
<textElement>
<font fontName="ArialMT" size="9"/>
</textElement>
<textFieldExpression><![CDATA[$F{typeFromZero}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="25" y="47" width="280" height="11" uuid="683e297f-1ac4-4d95-a9b3-4f033df9af09"/>
<textElement>
<font fontName="ArialMT" size="9"/>
</textElement>
<textFieldExpression><![CDATA[$F{type}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="25" y="74" width="515" height="33" uuid="bc3ccf2b-ad35-4c44-b228-2b9af97554b1"/>
<textElement>
<font fontName="ArialMT" size="9"/>
</textElement>
<textFieldExpression><![CDATA[$F{mappedExpenses}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
 
Now on my pdf I get values for "typeFromZero" and "mappedExpenses". mappedExpenses has value array and and everything as Json inside it like in the json: 
[
    {
      "uId": "lfpq3f0vfeqm2t9",
      "currencyId": "EUR",
      "type": "one",
    },
    {
      "uId": "plslhkxshfrpmn29",
      "currencyId": "EUR",
      "type": "two",
    }
  ]

But "type" has nothing. How can I repeat the types? Or actually how can I repeate all values from the array as sub bands/reports?

For creating the PDF in java I use:

JasperPrint jasperPrint =JasperFillManager.fillReport(JasperCompileManager.compileReport(inputStream), parameters, source);
JasperExportManager.exportReportToPdf(jasperPrint)

 

 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

  • 3 weeks later...

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...