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

Processing string arrays in JSON data file


allan.meek
Go to solution Solved by narcism,

Recommended Posts

I'm trying to display the contents of an array ("multi_values" in the JSON sample below) in a sub-report, but I'm missing something. The sub-report does display the correct number of Detail entries (i.e., 2 for item_name 'a' and 3 for item_name 'b'), so I know it's getting the array. However, despite numerous attempts, the subreport's textfield only ever displays "null".

I have a couple of ideas where things could be going wrong, but I've been unable to find any documentation or questions relating to such an array in JSON data:
* The multi_values field in the main report should have a different class (I've tried multiple other classes but nothing works)
* The multi_value field in the sub-report needs a different field.expression value (again, I've tried multiple things and just left "multi_value" here as a placeholder)
* The queryString in the sub-report needs to be something else

Any input would be greatly appreciated.

Here's the heavily reduced JSON:

{    "items": [        {            "item_name": "a",            "multi_values": ["value1", "value2"]        },        {            "item_name": "b",            "multi_values": ["value3", "value4", "value5"]        }    ]}

The main report:

<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="MultiValueExample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="988c6c04-ee0c-4e28-8121-de3584ad30b4">    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="MultiValueExample.xml"/>    <queryString language="jsonql">        <![CDATA[items]]>    </queryString>    <field name="item_name" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="item_name"/>        <fieldDescription><![CDATA[item_name]]></fieldDescription>    </field>    <field name="multi_values0" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="multi_values[0]"/>        <fieldDescription><![CDATA[multi_values0]]></fieldDescription>    </field>    <field name="multi_values" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="multi_values"/>        <fieldDescription><![CDATA[multi_values]]></fieldDescription>    </field>    <detail>        <band height="59" splitType="Stretch">            <textField>                <reportElement x="0" y="0" width="100" height="16" uuid="90cf147e-e35f-4a6a-8de2-87287688a229">                    <property name="com.jaspersoft.studio.unit.height" value="px"/>                </reportElement>                <textFieldExpression><![CDATA[$F{item_name}]]></textFieldExpression>            </textField>            <textField>                <reportElement x="110" y="0" width="100" height="16" uuid="90cf147e-e35f-4a6a-8de2-87287688a229">                    <property name="com.jaspersoft.studio.unit.height" value="px"/>                </reportElement>                <textFieldExpression><![CDATA[$F{multi_values0}]]></textFieldExpression>            </textField>            <subreport>                <reportElement x="0" y="18" width="480" height="40" uuid="cf743a67-8fcb-407d-9762-848428411202"/>                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)($P{REPORT_DATA_SOURCE})).subDataSource("multi_values")]]></dataSourceExpression>                <subreportExpression><![CDATA["MultiValueExampleSub.jasper"]]></subreportExpression>            </subreport>        </band>    </detail></jasperReport>

And the sub-report:

<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="MultiValueExampleSub" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6c426da9-4d08-46e0-a3fa-da99beedbeed">    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>    <queryString language="jsonql">        <![CDATA[]]>    </queryString>    <field name="multi_value" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="multi_value"/>        <fieldDescription><![CDATA[multi_value]]></fieldDescription>    </field>    <detail>        <band height="21">            <textField>                <reportElement x="0" y="0" width="280" height="16" uuid="c6e03595-dba0-49ea-bc8f-224fd23a45c9">                    <property name="com.jaspersoft.studio.unit.height" value="px"/>                </reportElement>                <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{multi_value}]]></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

Although not obvious, you need a "[0]" expression for your field in the subreport, like so:

<field name="multi_value" class="java.lang.String">  <property name="net.sf.jasperreports.jsonql.field.expression" value="[0]"/>  <fieldDescription><![CDATA[multi_value]]></fieldDescription></field>[/code]

If you want to access the array values in a more concise way without using subreports, you could iterate directly over the multi_values items then travel up the JSON tree for the other values:

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.8.0.final using JasperReports Library version 6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b  --><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" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="76779812-15b1-4e44-ad29-10e660c7e020">    <queryString language="jsonql">        <![CDATA[..multi_values.*]]>    </queryString>    <field name="value" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="[0]"/>        <fieldDescription><![CDATA[Value]]></fieldDescription>    </field>    <field name="itemName" class="java.lang.String">        <property name="net.sf.jasperreports.jsonql.field.expression" value="^{2}.item_name"/>        <fieldDescription><![CDATA[item name]]></fieldDescription>    </field>    <background>        <band splitType="Stretch"/>    </background>    <columnHeader>        <band height="30" splitType="Stretch">            <staticText>                <reportElement x="277" y="0" width="278" height="30" uuid="fd4f82c2-abbe-4ef8-8eb8-91afd9db6c46">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="0499f481-103c-4059-a47e-03973f8e0a26"/>                </reportElement>                <text><![CDATA[Value]]></text>            </staticText>            <staticText>                <reportElement x="0" y="0" width="277" height="30" uuid="3a9080a0-6b05-4ec9-a0d3-fec572c39796">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="95758653-15a1-4f2f-818e-073c28049db5"/>                </reportElement>                <text><![CDATA[item Name]]></text>            </staticText>        </band>    </columnHeader>    <detail>        <band height="30" splitType="Stretch">            <textField>                <reportElement x="277" y="0" width="278" height="30" uuid="5241ef9c-fea3-499f-bf4a-58434d8872e2">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="0499f481-103c-4059-a47e-03973f8e0a26"/>                </reportElement>                <textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>            </textField>            <textField>                <reportElement x="0" y="0" width="277" height="30" uuid="baaf49be-1cd2-42f0-b8fb-948146979e58">                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="95758653-15a1-4f2f-818e-073c28049db5"/>                </reportElement>                <textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression>            </textField>        </band>    </detail></jasperReport>[/code]

 

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