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

Can't create a list field with JSON Datasource


inanutshellus
Go to solution Solved by inanutshellus,

Recommended Posts

 

Given a simple JSON datasource, like so: 

 

 

    {
      "Customers": [
        {
          "ContactName": "John Doe",
          "Orders": [
            { "OrderID": 100 },
            { "OrderID": 101 }
          ]
        }, {
          "ContactName": "Jane Smith",
          "Orders": [
            { "OrderID": 102 },
            { "OrderID": 103 },
            { "OrderID": 104 }
          ]
        }
      ]
    }
 

 

I can't seem to declare the field "Orders" as a List. 

 

It blows up like so: 

 

 

    Error filling print... Unable to get value for field 'Orders' of class 'java.util.List'
    net.sf.jasperreports.engine.JRException: Unable to get value for field 'Orders' of class 'java.util.List'
         at net.sf.jasperreports.engine.data.JsonDataSource.getFieldValue(JsonDataSource.java:233)
         at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1317)
    [...]
         at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997) Caused by: net.sf.jasperreports.engine.JRException: Field 'Orders' is of class 'java.util.List' and cannot be converted
         at net.sf.jasperreports.engine.data.JsonDataSource.getFieldValue(JsonDataSource.java:230)
         ... 13 more
 

 

 

To reproduce it, load the following JRXML into iReport, and try to run it with the above datasource. Notice I'm not even using the Orders!

 

 

    <?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="deepJsonTest" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5f9dde08-ee77-4cc0-bfe5-199306a133a9">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <queryString language="json">
            <![CDATA[Customers]]>
        </queryString>
        <field name="ContactName" class="java.lang.String">
            <fieldDescription><![CDATA[ContactName]]></fieldDescription>
        </field>
        <field name="Orders" class="java.util.List">
            <fieldDescription><![CDATA[Orders]]></fieldDescription>
        </field>
        <detail>
            <band height="153" splitType="Stretch">
                <textField>
                    <reportElement uuid="923b522c-825e-4ebf-a3be-42802544f48f" x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{ContactName}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
 
Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution

Looking in the source, turns out they don't support lists, so you can't do subdatasets without a flat file (which is how the subdataset is done in the Jasper Reports example).

However, one of my devs has dropped a patch here:

http://community.jaspersoft.com/jasperreports-library/issues/2500-0

If you have a JSON List, declare it as an InputStream and send it to your subdataset. All we need now is someone to review the patch.

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