title: How to create subgroups using JSON as datasource?
body: <p>I would like to have a report where shows my <code>Orders</code> classified, grouped, by <code>Currency</code>, and by <code>Country</code>.</p>
  
  <p>It should look like this:</p>
  
  <pre><code>EUR
   - Germany
     - (only orders with EUR as currency and order made in Germany)
   - France
     - (only orders with EUR as currency and order made in France)
  
  US
    - USA
     - (only orders with US as currency and order made in USA)
  </code></pre>
  
  <p>So, I thought creating a <code>master</code> report, which will have 2 <code>subreports</code>:</p>
  
  <pre><code>master
    - subreport grouping orders by currency
      - subreport grouping orders by country
        - subreport listing the orders
  </code></pre>
  
  <p>So I did it like this:</p>
  
  <p><strong>master.jrxml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  --&gt;
  &lt;!-- 2016-11-18T18:37:36 --&gt;
  &lt;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="master" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50" uuid="bbe115b5-a5a0-4b39-9b73-7092dc59ab6d"&gt;
      &lt;property name="com.jaspersoft.studio.data.defaultdataadapter" value="Demo JSON Data Adapter "/&gt;
      &lt;property name="net.sf.jasperreports.json.source" value="/home/vasi/git/valter/reports/Demo/data/northwind.json"/&gt;
      &lt;style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/&gt;
      &lt;style name="Sans_Normal_8" style="Sans_Normal" fontSize="8"/&gt;
      &lt;style name="Sans_Bold" fontName="DejaVu Sans" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/&gt;
      &lt;style name="Sans_Bold_8" style="Sans_Bold" fontSize="8"/&gt;
      &lt;queryString language="json"&gt;
          &lt;![CDATA[Northwind]]&gt;
      &lt;/queryString&gt;
      &lt;field name="Customers" class="java.lang.String"&gt;
          &lt;fieldDescription&gt;&lt;![CDATA[Customers]]&gt;&lt;/fieldDescription&gt;
      &lt;/field&gt;
      &lt;field name="Orders" class="java.lang.String"&gt;
          &lt;fieldDescription&gt;&lt;![CDATA[Orders]]&gt;&lt;/fieldDescription&gt;
      &lt;/field&gt;
      &lt;title&gt;
          &lt;band height="50"&gt;
              &lt;line&gt;
                  &lt;reportElement x="0" y="0" width="515" height="1" uuid="fc148b4e-50df-4a12-aa14-8505a4cfa6e1"/&gt;
              &lt;/line&gt;
              &lt;staticText&gt;
                  &lt;reportElement style="Sans_Normal" x="0" y="10" width="515" height="30" uuid="5bf7651c-cd6b-4eaf-b65a-1413d60faab0"/&gt;
                  &lt;textElement textAlignment="Center"&gt;
                      &lt;font size="22"/&gt;
                  &lt;/textElement&gt;
                  &lt;text&gt;&lt;![CDATA[Orders sorted by currency and country]]&gt;&lt;/text&gt;
              &lt;/staticText&gt;
          &lt;/band&gt;
      &lt;/title&gt;
      &lt;detail&gt;
          &lt;band height="70"&gt;
              &lt;subreport isUsingCache="true"&gt;
                  &lt;reportElement x="0" y="0" width="515" height="70" uuid="ebd4e147-b294-4367-a072-dc1954980232"/&gt;
                  &lt;subreportParameter name="REPORT_DATA_SOURCE"&gt;
                      &lt;subreportParameterExpression&gt;&lt;![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Orders")]]&gt;&lt;/subreportParameterExpression&gt;
                  &lt;/subreportParameter&gt;
                  &lt;subreportExpression&gt;&lt;![CDATA["reports/orders/subreport/currency.jasper"]]&gt;&lt;/subreportExpression&gt;
              &lt;/subreport&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
  &lt;/jasperReport&gt;
  </code></pre>
  
  <p><strong>subreport/currency.jrxml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  --&gt;
  &lt;!-- 2016-11-18T18:38:23 --&gt;
  &lt;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="currency" pageWidth="515" pageHeight="742" columnWidth="515" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="bbe115b5-a5a0-4b39-9b73-7092dc59ab6d"&gt;
      &lt;style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/&gt;
      &lt;style name="Sans_Normal_8" style="Sans_Normal" fontSize="8"/&gt;
      &lt;style name="Sans_Bold" fontName="DejaVu Sans" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/&gt;
      &lt;style name="Sans_Bold_8" style="Sans_Bold" fontSize="8"/&gt;
      &lt;field name="Currency" class="java.lang.String"&gt;
          &lt;property name="net.sf.jasperreports.json.field.expression" value="Currency"/&gt;
      &lt;/field&gt;
      &lt;sortField name="Currency"/&gt;
      &lt;group name="CurrencyGroup" isReprintHeaderOnEachPage="true"&gt;
          &lt;groupExpression&gt;&lt;![CDATA[$F{Currency}]]&gt;&lt;/groupExpression&gt;
          &lt;groupHeader&gt;
              &lt;band height="21"&gt;
                  &lt;textField&gt;
                      &lt;reportElement style="Sans_Bold" x="5" y="5" width="100" height="15" isPrintWhenDetailOverflows="true" uuid="0aaeed6e-7ba1-4ab9-be59-d6ca702995fc"/&gt;
                      &lt;textFieldExpression&gt;&lt;![CDATA[$F{Currency}]]&gt;&lt;/textFieldExpression&gt;
                  &lt;/textField&gt;
                  &lt;line&gt;
                      &lt;reportElement x="0" y="20" width="515" height="1" isPrintWhenDetailOverflows="true" uuid="6d76c22c-329f-4e77-a886-8580d3cb6bc1"/&gt;
                  &lt;/line&gt;
              &lt;/band&gt;
          &lt;/groupHeader&gt;
          &lt;groupFooter&gt;
              &lt;band height="31"/&gt;
          &lt;/groupFooter&gt;
      &lt;/group&gt;
      &lt;pageHeader&gt;
          &lt;band height="21"&gt;
              &lt;staticText&gt;
                  &lt;reportElement style="Sans_Bold" mode="Opaque" x="0" y="5" width="515" height="15" forecolor="#FFFFFF" backcolor="#333333" uuid="da0f1cad-f552-424b-bf19-b41cabbfa4ac"/&gt;
                  &lt;text&gt;&lt;![CDATA[Currency]]&gt;&lt;/text&gt;
              &lt;/staticText&gt;
          &lt;/band&gt;
      &lt;/pageHeader&gt;
      &lt;detail&gt;
          &lt;band height="60"&gt;
              &lt;subreport&gt;
                  &lt;reportElement x="10" y="10" width="490" height="40" uuid="d181a0e1-1a31-4723-abe7-8f2f3dc09bce"/&gt;
                  &lt;subreportParameter name="REPORT_DATA_SOURCE"&gt;
                      &lt;subreportParameterExpression&gt;&lt;![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]&gt;&lt;/subreportParameterExpression&gt;
                  &lt;/subreportParameter&gt;
                  &lt;subreportExpression&gt;&lt;![CDATA["reports/orders/subreport/subreport/countries.jasper"]]&gt;&lt;/subreportExpression&gt;
              &lt;/subreport&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
  &lt;/jasperReport&gt;
  </code></pre>
  
  <p>However, I don't know how to pass my datasource from my subreport to my subreport. From <code>master</code> report to my subreport it was okay, but I'm stuck with this exception:</p>
  
  <pre><code>net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()
      at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:506)
      at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:481)
      at com.jaspersoft.studio.editor.preview.view.control.ReportControler$5.run(ReportControler.java:362)
      at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
  Caused by: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()
      at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:872)
      at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:330)
      at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:382)
      at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:357)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2031)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
      at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:119)
      at java.lang.Thread.run(Unknown Source)
  Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()
      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:264)
      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:610)
      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:578)
      at net.sf.jasperreports.engine.fill.JRFillSubreport.getParameterValues(JRFillSubreport.java:680)
      at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateParameterValues(JRFillSubreport.java:478)
      at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:458)
      at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:345)
      at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258)
      at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:454)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2029)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
      at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:736)
      at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:58)
      at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:216)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      ... 1 more
  Caused by: java.lang.ClassCastException: net.sf.jasperreports.engine.fill.SortedDataSource cannot be cast to net.sf.jasperreports.engine.data.JsonDataSource
      at currency_1479490219429_385194.evaluate(currency_1479490219429_385194:195)
      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:251)
      ... 19 more
  </code></pre>
  
  <p>I've <a href="https://github.com/valterhenrique/reports/tree/stackoverflow_40683029" rel="nofollow noreferrer">created a branch</a> only for this question. This <a href="https://github.com/valterhenrique/reports/blob/stackoverflow_40683029/Demo/data/northwind.json" rel="nofollow noreferrer">is my json data source</a>.</p>
  
  <p>I've also tried <a href="https://stackoverflow.com/questions/8490563/how-to-pass-main-report-data-source-to-subreport-jasperreports">this suggestion</a>, however, I don't know how to <em>get</em> my data source as parameter on my subreport, and how to re-send it again. :(</p>
  
  <p>Any ideas, or sugesstions, would be very appreciated!</p>