How to send a collection of data from a JasperReports template to a scriptlet?


Mostly we've been accepting json data into a multitude of JasperReports templates, picking out individual fields and displaying them.

We now have a new requirement that involves accessing a collection within the json data rather than using the individual fields. It’s proving difficult to do.

Our aim is to have each a heading for each different method and then add a bulleted list of types associated with a particular method underneath.

I have set up a scriptlet that would work through the array and assemble a string that could be displayed in the JasperReports template.

However, I’m struggling to get the collection of data into the scriptlet.

I’ve proven the connection works as I can send through a string (from the template to the scriptlet), tweak it and send it back for display.

Unfortunately I can't seem to access a collection as a variable/field etc in the template and then send it through to the java scriptlet. I've got to the point where the template at least compiles with a field/variable defined with class ‘java.util.Arraylist’, but it that field/variable never contains any of the data from the json.

So I either need a clue as to how to define all this properly, assuming it is possible and I've just done it wrong. Or a way to get access to the whole datasource within the java scriptlet and then pick through for the bits I want in there.

The template I've used for testing:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version using JasperReports Library version 6.1.1  -->
<jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" name="reportNewClaim_subreport_evidenceList_uploads" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="43ef5573-1a4b-4c0d-a713-ecc72a8b64ee">
   <property name="ireport.zoom" value="3.1384283767210035"/>
   <property name="ireport.x" value="304"/>
   <property name="ireport.y" value="0"/>
   <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>
   <property name="" value="true"/>
   <template><![CDATA[$P{TEMPLATE_DIR} + "StyleTemplateClaim.jrtx"]]></template>
   <scriptlet name="uploadsScriptlet" class="" />
   <parameter name="SUBREPORT_DIR" class="java.lang.String">
   <parameter name="TEMPLATE_DIR" class="java.lang.String">
   <queryString language="JSON">
   <field name="firstEvidence" class="java.lang.String">
   <field name="firstEvidence1" class="java.lang.String">
   <field name="teststring" class="java.util.ArrayList">
   <variable name="variable1" class="java.util.ArrayList" calculation="System" resetType="Report">
      <initialValueExpression><![CDATA[new ArrayList()]]>
   <variable name="evidencefromscriptlet" class="java.lang.String" calculation="System" />
      <band splitType="Stretch"/>
      <band height="18" splitType="Stretch">
         <property name="" value="pixel"/>
         <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement positionType="Float" x="0" y="0" width="595" height="18" isRemoveLineWhenBlank="true" uuid="d1f681c7-12c0-4696-afcf-2d7be4833b58">
               <bottomPen lineWidth="0.0"/>
               <font isBold="true"/>
               <paragraph lineSpacing="1_1_2"/>
            <textFieldExpression><![CDATA["**** "+ ($V{variable1}).toString() + "**" + ($F{firstEvidence1}).toString() + "**"+ $F{firstEvidence} + "***" +$V{evidencefromscriptlet}]]></textFieldExpression>
The scriptlet:
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import java.util.ArrayList;
import java.util.List;
public class UploadsScriptlet extends JRDefaultScriptlet {
    public void afterReportInit() throws JRScriptletException {
        List<Evidence> testList = (List<Evidence>) getVariableValue("variable1");
        String test = "temp";
        for (Evidence evidence : testList) {
            test += evidence.getEvidenceType();
            test += " ";
        setVariableValue("evidencefromscriptlet", test);


    "axxx": {
        "head": {
            "dateTimeSubmitted": "01-Jul-2019 15:08:50",
            "language": "en",
            "origin": "gb"
        "body": {
            "data": {
                "aboutYou": {
                    "Title": {
                        "question": "Title",
                        "answer": "Mr"
                    "FirstName": {
                        "question": "First name",
                        "answer": "David"
                    "MiddleName": {
                        "question": "Middle name(s)",
                        "answer": "John"
                    "LastName": {
                        "question": "Last name",
                        "answer": "Smith"
        "upload": {
            "evidence": [
                    "type": "type1",
                    "method": "method1",
                    "files": [
                            "name": "a.jpg",
                            "id": "id1"
                    "type": "type2",
                    "method": "method2",
                    "files": []
                    "type": "type3",
                    "method": "method3",
                    "files": []
            "groupId": "grpId1"

f.e.evans's picture
Joined: Oct 11 2017 - 5:32am
Last seen: 1 month 1 week ago

0 Answers:

No answers yet