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

Need help looping through JSON


brossow

Recommended Posts

I am trying to create a report in iReport Designer v5.6.0 that loops through a JSON file and returns all data from different levels.  For example, I've built a very simple JSON file that has a list of customers, and for each customer it lists all of his/her vehicles including fields for make, model and mileage.  The desired output would be a list of customers and underneath each customer a list of the vehicles he/she owns.

I have built a template that correctly displays each customer name, but I have been unable to figure out how to loop through each customer's vehicles.  (I can return the first vehicle by adding [0] to the field, but I can't figure out how to loop through all vehicles.)

JRXML:

[/code]
<?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="report1" language="groovy" pageWidth="612" pageHeight="792" columnWidth="540" leftMargin="36" rightMargin="36" topMargin="36" bottomMargin="36" uuid="20a5fd88-b322-439b-b413-75d8da29ff65">
<property name="net.sf.jasperreports.json.source" value="simple_json_cars.json"/>
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="custName" class="java.lang.String">
<fieldDescription><![CDATA[custName]]></fieldDescription>
</field>
<field name="carMake" class="java.lang.String">
<fieldDescription><![CDATA[custCars[0].carMake]]></fieldDescription>
</field>
<field name="carModel" class="java.lang.String">
<fieldDescription><![CDATA[custCars[0].carModel]]></fieldDescription>
</field>
<field name="carMiles" class="java.lang.Double">
<fieldDescription><![CDATA[custCars[0].carMiles]]></fieldDescription>
</field>
<detail>
<band height="60" splitType="Prevent">
<textField isStretchWithOverflow="true">
<reportElement x="0" y="0" width="540" height="18" uuid="b96f609f-80ad-4fa1-a92a-55ccf9eb9fcc"/>
<textFieldExpression><![CDATA[$F{custName}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="90" y="18" width="450" height="12" uuid="dc88d116-bfaf-4495-bf5c-74065aa798bd"/>
<textFieldExpression><![CDATA[$F{carMake} + " " + $F{carModel}]]></textFieldExpression>
</textField>
<textField pattern="#,##0.0">
<reportElement x="90" y="30" width="450" height="12" uuid="c5f47711-29f8-425d-a2d7-23bb26bc2d0f"/>
<textFieldExpression><![CDATA[$F{carMiles}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="36" y="18" width="54" height="12" uuid="042eb8b6-ee5c-40e4-a683-b36ade763b66"/>
<textFieldExpression><![CDATA["Vehicle:"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="36" y="30" width="54" height="12" uuid="bb446f69-b731-4221-b9d5-a9186282ad4b"/>
<textFieldExpression><![CDATA["Miles:"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
 

JSON:

[/code]
[
{
"custName": "Alison Anderson",
"custCars":[
{
"carMake": "Chevrolet",
"carModel": "Avalanche",
"carMiles": 97740
},
{
"carMake": "Chevrolet",
"carModel": "Beretta",
"carMiles": 165500
}
 ]
},
{
"custName": "Bart Bloomfield",
"custCars":[
{
"carMake": "Ford",
"carModel": "F150",
"carMiles": 43252
},
{
"carMake": "Mercury",
"carModel": "Marauder",
"carMiles": 26699
}
 ]
},
{
"custName": "Charles Cominsky",
"custCars":[
{
"carMake": "Porsche",
"carModel": "Cayman",
"carMiles": 15520
}
 ]
}
]
 

Any help is greatly appreciated.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Disregard.  I was able to solve my problem my rearranging the JSON and flattening it out more.  By passing a new (in this bogus example) customer ID field to the vehicle nodes, I can group by customer ID and it loops through the vehicles beautifully.  The moral of the story is that sometimes the answer isn't to change the template but rather to change the data its being fed.

Link to comment
Share on other sites

  • 1 year 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...