JSON data not loading in studio or library

0
With the Java code below and jasperreports studio, I am trying to pass the JSON to the report but the data is not populating.  The static text will appear but not the JSON data.  
 
If you see below, I am trying to access isSuccessful but no data is returned.  Actually, I can't find a reference to any of the fields using the JSON expression in the xml.
 
And JSON:
 
{  
  "calcData": {
    "data": {
      "savingsFrequencyIndicator": 12,
      "saveEachIndicator": 12,     
      "schedulechart": [
         {
           "month": 1,
           "savings": 123.00,
           "balance": 1430.00
         },
         {
       "month": 2,
       "savings": 123.00,
       "balance": 1560.88
         }
      ]
    },
    "type": "savingsData"
  },
  "isSuccessful": true 
}
 
 
 iss = new ClassPathResource("/com/ca/services/reportgencore/api/testnorthworks.json").getInputStream();            
            final StringWriter writer = new StringWriter();
            IOUtils.copy(iss, writer, "UTF-8");
            final String jsonAsString = writer.toString();
            System.out.println("JSON as input >>>: "+jsonAsString);            
            targetStream = new ByteArrayInputStream(jsonAsString.getBytes());
 
            // Build a JSON Datasource //
            final JsonDataSource jsonStreamDataSource = new JsonDataSource(targetStream);
 
            params.put(JsonQueryExecuterFactory.JSON_DATE_PATTERN, "yyyy-MM-dd");
            params.put(JsonQueryExecuterFactory.JSON_NUMBER_PATTERN, "#,##0.##");
            params.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.US);
            params.put(JRParameter.REPORT_LOCALE, Locale.US);            
            params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, targetStream);
 
            // Load the compiled template system and output the PDF document
            final JasperReport report = TestJasperReportUtil.loadJASPERFile("/com/ca/services/reportgencore/api/SimpleDataJust.jasper");
            final JasperPrint jasperPrint = JasperFillManager.fillReport(report, params, jsonStreamDataSource);
            pdf = JasperExportManager.exportReportToPdf(jasperPrint);      
 
 
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2019-02-12T13:27:48 -->
<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="JsonOrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="5a62986f-c97c-4e2f-b4ed-d9f38960dab4">
    <property name="net.sf.jasperreports.json.source" value="data/example_fincalc_data.json"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="FincalcNewMock"/>
    <queryString language="json">
        <![CDATA[.]]>
    </queryString>
    <field name="Id" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="OrderID"/>
    </field>
    <field name="isSuccessful" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="isSuccessful"/>
    </field>
    <pageHeader>
        <band height="14">
            <frame>
                <reportElement mode="Opaque" x="0" y="2" width="356" height="10" forecolor="#CCFFFF" backcolor="#CCFFFF" uuid="e9af134f-31eb-48be-bd9b-292188f2554f"/>
                <staticText>
                    <reportElement mode="Opaque" x="0" y="0" width="48" height="10" backcolor="#CCFFFF" uuid="62e5e770-7b05-4ecd-a254-ab0c7f643a37"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[ID]]></text>
                </staticText>
            </frame>
        </band>
    </pageHeader>
    <detail>
        <band height="14">
            <textField>
                <reportElement x="0" y="2" width="51" height="10" uuid="ec54687d-3c95-4647-9db5-fa71a6e81009"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="77">
            <staticText>
                <reportElement x="20" y="20" width="100" height="30" uuid="15321182-a6c8-494d-ba0f-56e71987d588"/>
                <text><![CDATA[Beginning Balance:]]></text>
            </staticText>
            <staticText>
                <reportElement x="190" y="20" width="100" height="30" uuid="e09a4690-13a6-4c85-915e-4c75a091dfd4"/>
                <text><![CDATA[Simple Data 2]]></text>
            </staticText>
            <textField>
                <reportElement key="" x="130" y="33" width="51" height="10" uuid="b055a4bd-cdd8-4b82-8690-6b21055f5212"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{isSuccessful}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>
 
 
And JSON input that isn't loading:
berlin.brown's picture
Joined: Feb 12 2019 - 10:18am
Last seen: 6 months 1 week ago

Fixed by matching up the json name exactly as the name of the field

berlin.brown - 6 months 2 weeks ago

1 Answer:

0

Your textFields displaying data are too small for showing anything. Either try:

- setting 'Stretch With Overflow' in the 'Text Field' tab of the textField's properties panel

- or gradually lower the font size on the textField untill you see the data

- or increase the textField's height

EDIT: Since I cannot test the code that you are running your report with, I can only mention some guidelines:

- make sure your json file is in classpath

- remove this line from your JRXML as it might attempt to load the json data from there, in case you pass a null InputStream: 

 <property name="net.sf.jasperreports.json.source" value="data/example_fincalc_data.json"/>

- or you could just set this parameter in your parameters map:

params.put(JsonQueryExecuterFactory.JSON_SOURCE, "classpath/to/json/file");

- you don't need to construct a JsonDatasource instance for filling the report as it will be constructed automatically based on the parameters you've just passed

In essence your code should look simpler, like so:

params.put(JsonQueryExecuterFactory.JSON_DATE_PATTERN, "yyyy-MM-dd");
params.put(JsonQueryExecuterFactory.JSON_NUMBER_PATTERN, "#,##0.##");
params.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.US);
params.put(JRParameter.REPORT_LOCALE, Locale.US);            
params.put(JsonQueryExecuterFactory.JSON_SOURCE, "com/ca/services/reportgencore/api/testnorthworks.json");
 
// Load the compiled template system and output the PDF document
final JasperReport report = TestJasperReportUtil.loadJASPERFile("/com/ca/services/reportgencore/api/SimpleDataJust.jasper");
final JasperPrint jasperPrint = JasperFillManager.fillReport(report, params);
pdf = JasperExportManager.exportReportToPdf(jasperPrint);

narcism's picture
1085
Joined: Nov 22 2010 - 12:39am
Last seen: 15 hours 51 min ago

That fixed the first problem, thanks.

 

The studio is populating data from the JSON properly.

 

How do I get the Java code to populate.  What code do I use,

 

I tried the following, 

 final String jsonAsString =  mapper.writeValueAsString(recordJsonObjectAfterDecorate.getDataadapter());
            logger.info("Reading JSON data into jasper report library: " + jsonAsString);
            logger.info("Loading report, "+templateresourcefinder);
 
            targetStream = new ByteArrayInputStream(jsonAsString.getBytes());
 
            // Build a JSON Datasource //
            final JsonDataSource jsonStreamDataSource = new JsonDataSource(targetStream);           
            params.put(JsonQueryExecuterFactory.JSON_DATE_PATTERN, "yyyy-MM-dd");
            params.put(JsonQueryExecuterFactory.JSON_NUMBER_PATTERN, "#,##0.##");
            params.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.US);
            params.put(JRParameter.REPORT_LOCALE, Locale.US);            
            params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, targetStream);

Do you see an issue with the Java code same tempalte file?

berlin.brown - 8 months 1 week ago

Please see the EDIT section in my answer

narcism - 8 months 1 week ago

I am trying to use a JSON stream.  It is kind of strange, some JSON data gets loaded but some doesn't.

berlin.brown - 8 months 1 week ago
show 1 more...

I got it to work, but I had to use this format,

The field name had to match the JSON expression.

<field name="saving.beginningBalance" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="saving.beginningBalance"/>
    </field>
    

berlin.brown - 8 months 5 days ago
Feedback