jsonjaspersoft-studio

Adding JSON items to a Jaspersoft report by index


I have a JSON array like this:

"platDist": [

{"name": "Wordpress", "total": 1360804},

{"name": "Bespoke", "total": 562864},

What I am trying to achieve is to have each name and total in separate text fields in a Jaspersoft report instead of displaying the whole thing in a table or list.

I have created a new report and attached a Data Adapter containing the JSON to it. I can drag the 'name' field into the Detail 1 band and it appears as $F{name}, showing 'Wordpress' and all the other names.

<textField>

<reportElement x="319" y="51" width="100" height="30" uuid="a76b2855-88bc-49ea-a70d-15d86d8f3c50">

<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="0c07b6de-eb6f-4c5b-a466-9020d88cf7f4"/>

</reportElement>

<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>

</textField>

In my head, what I would like to do is have them all positioned where I want them, and I imagined it would be something as simple as

<textFieldExpression><![CDATA[$F{name[0]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[1]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[2]}]]></textFieldExpression>

which, obviously, doesn't work. I can't believe this isn't possible, but I am more of a front end dev with very little experience of data connections, so a lot of the complexity of Jaspersoft is going over my head. Any help or guidance on how to do this will be very much appreciated. TIA.


Solution

  • In the most simple case this is achievable by:

    Considering this JSON:

    {
        "platDist": [
            {"name": "Wordpress", "total": 1360804},
            {"name": "Bespoke", "total": 562864},
            {"name": "Other", "total": 12345}
        ]
    }
    

    here's a complete example:

    <?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="Report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3657c5d3-8f47-4baf-8261-21d46fd36db8">
        <queryString language="jsonql">
            <![CDATA[]]>
        </queryString>
        <field name="Name1" class="java.lang.String">
            <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[0].name"/>
        </field>
        <field name="Name2" class="java.lang.String">
            <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[1].name"/>
        </field>
        <field name="Name3" class="java.lang.String">
            <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[2].name"/>
        </field>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="79" splitType="Stretch">
                <staticText>
                    <reportElement x="170" y="20" width="220" height="30" uuid="5240258f-73d0-4672-bda1-0e6cee344fe1"/>
                    <textElement textAlignment="Center">
                        <font size="14"/>
                    </textElement>
                    <text><![CDATA[Indexed textField expression]]></text>
                </staticText>
            </band>
        </title>
        <summary>
            <band height="161" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                    <text><![CDATA[Name1]]></text>
                </staticText>
                <textField>
                    <reportElement mode="Opaque" x="80" y="0" width="100" height="30" backcolor="#DEFBFF" uuid="59ff204c-5118-40fd-89d1-92b6433d8e0e"/>
                    <textFieldExpression><![CDATA[$F{Name1}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement x="0" y="40" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                    <text><![CDATA[Name2]]></text>
                </staticText>
                <textField>
                    <reportElement mode="Opaque" x="80" y="40" width="100" height="30" backcolor="#BDFBFC" uuid="e4fc53a7-458c-48af-874b-d1c9cda2fba3"/>
                    <textFieldExpression><![CDATA[$F{Name2}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement x="0" y="80" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                    <text><![CDATA[Name3]]></text>
                </staticText>
                <textField>
                    <reportElement mode="Opaque" x="80" y="80" width="100" height="30" backcolor="#64E1FA" uuid="8368b190-d736-43be-a7ec-45fb0db865a8"/>
                    <textFieldExpression><![CDATA[$F{Name3}]]></textFieldExpression>
                </textField>
            </band>
        </summary>
    </jasperReport>
    

    that produces this output: enter image description here

    A note: because this sample does not contain a detail band, in order for the output to still be produced, setting whenNoDataType="AllSectionsNoDetail" at the report level is necessary.