Jump to content
We've recently updated our Privacy Statement, available here ×
  • Subreport With Data From SQL Query Which Is In JSON Format


    akovach
    • Features: Data Sources, JasperReports Server, Reports Version: v7.1, v6.4.2 Product: JasperReports Server JSON Data Source

    Issue Description

    User wants to create a subreport with data from main report SQL query which is in JSON format.


    Resolution

    To create such report we would need to create to reports: main report (MainReport.jrxml) and subreport(SubReport.jrxml). Main have ordinary SQL Query which gets data from DB Table. This query can be represented by the following statement:

    SELECT "json_text" , "id"
    FROM 
    (VALUES 
    ('[{ 
    ''dateFrom'': ''01.12.2016'', 
    ''dateTo'': ''01.12.2016'', 
    ''someOptionalNumber'': 12 
    },{ 
    ''dateFrom'': ''01.12.2016'', 
    ''dateTo'': ''01.12.2016'' 
    }, { 
    ''dateFrom'': ''01.12.2016'', 
    ''dateTo'': ''01.12.2016'', 
    ''someOptionalNumber'': 11 
    }]', 1)
    ) s("json_text" , "id")
    

    The structure of main report is simple: a Title and a Subreport element in Summary band

    mainreport_0.png.19ed07a29efc4e080fae473f7881eb0f.png

    Subreport element should contain several parameters: 'JSON_INPUT_STREAM' and 'net.sf.jasperreports.json.date.pattern'.  'JSON_INPUT_STREAM' is needed to process JSON String from the main report field and pass it to subreport.  'net.sf.jasperreports.json.date.pattern' is used as a pattern for dates in subreport. The code for subreport element:

    <subreport>
        <reportElement x="-1" y="0" width="545" height="320" uuid="b11b1e89-b446-44a6-aa9e-c12fdba2d428"/>
        <subreportParameter name="JSON_INPUT_STREAM">
            <subreportParameterExpression><![CDATA[new ByteArrayInputStream($F{json_text}.getBytes("UTF-8"))]]></subreportParameterExpression>
        </subreportParameter>
        <subreportParameter name="net.sf.jasperreports.json.date.pattern">
            <subreportParameterExpression><![CDATA["d.m.y"]]></subreportParameterExpression>
        </subreportParameter>
        <subreportExpression><![CDATA["Blank_A4.jasper"]]></subreportExpression>
    </subreport>

    Subreport should have query language 'JSON':

    <queryString language="json">
            <![CDATA[]]>
    </queryString>
    

    Please make sure, that subreport contains all actual fields, which are in JSON String (in this case: "dateFrom", "dateTo", "someOptionalNumber"):

    subreport.png.18a3c018e45362d2de8b7fb6dfc17699.png

    Now we can publish main report to JasperReports Server to check the result. As you can see, JSON String was passed to subreport and parsed correctly by JSON executer:

    result_0.png.f6dbee8a446c47b00e5a52aa5a75a628.png

    mainreport.jrxml

    subreport.jrxml


    User Feedback

    Recommended Comments

    There are no comments to display.



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