Jump to content
We've recently updated our Privacy Statement, available here ×
  • Creating a Report using JSON data from a parameter (with subreports)


    Scott Andersen
    • Features: Reports Version: v7.5.0 Product: Jaspersoft® Studio

    You can create a report with all the data coming in as a parameter in JSON format. HEre is how you do it.

    • Put sample data with basic structure of the JSON data you are reporting on into a file. This facilitates the creation of and testing of sub reports / reports. (See attached example)
    • Design the subreport(s) using the file as a data source. This makes creating the fields easier. It also helps you test the JSON query to get at the data you want to reach. You can create the fields directly but it is quite involved.
      • Query the JSON to get the structure you want.
      • Generate the fields.
        OR
      • Create fields individually. Field name, description, Property – field expression;
    • Design and compile your sub  report(s)
    • Create the master report
    • Create a string parameter that passes the json data into the parameter. Set the initial value to the test JSON structure as a string. (jsonData)
    • Create parameter “JSON_INPUT_STREAM”, class java.io.InputStream, uncheck “Is for prompting”, default value expression:
      new java.io.ByteArrayInputStream($P{jsonData}.getBytes("UTF8"))
        • $P{jsonData} Is the json parameter you created above.
    • Add the subreport.
      • Edit Parameters.
      • Add parameter “REPORT_DATA_SOURCE”, set expression to
        new net.sf.jasperreports.engine.data.JsonDataSource( new java.io.ByteArrayInputStream( $P{jsonData}.getBytes("UTF-8") ), ".")
        • $P{jsonData} is the json parameter you created above.
        • “.” Is the query path into the data used in the subreport.

    json_parameter_report.zip

    testdata1.json


    User Feedback

    Recommended Comments

    I do not understand what it is needed for. Can somebody enlighten me on this one? Thanks /sites/all/libraries/ckeditor/plugins/smiley/images/regular_smile.png

    So far I did not get it to work. It would have been more useful when the example files were more focused on the task to accomplish.

    Link to comment
    Share on other sites

    The JSON_INPUT_STREAM parameter establishes access to the json parameter when using a JSON query.  

    The example includes subreports that handle the actual data. This allows you to query the original string for values.

    Link to comment
    Share on other sites

    I have a doubt. Where we do the following??? In the report??? Or in the API as json-data-source???

    • Quote

      Create a string parameter that passes the json data into the parameter. Set the initial value to the test JSON structure as a string. (jsonData)

       

    Link to comment
    Share on other sites

    23 hours ago, lgameror said:

    I have a doubt. Where we do the following??? In the report??? Or in the API as json-data-source???

    Yes, you will need to create the jsonData parameter in the report template, the JRXML. You will typically do that using Jaspersoft Studio

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