Search code examples
jsonjasper-reports

Use a different JSON datasource in a jasper subreport and master report


Hi before I explain my problem I will tell you what I am trying to acocmplish.

I am trying to create a subreport that shows the different incomes of a user. A user can recieve multiple donations, have sold multiple house/properties, won contests etc. All of these actions have more details on how much money they recieved, taxes paid,...

Now I am trying to show the details of a user (name, surname,..;) in one detail. This works. the next detail band would be for donations. I created a subreport in which I specified the layout for this section. However the datasource I need for this subreport is different then the one from my main report. Is there a way to pass a new data source (JSON) to my subreport or pass a list object extracted from my original data source so only my detail band for donations is repeated? THen another datasource (or list from my masterreports data source) to repeat the sales detail band and so on for every option.

the final layout would look something like this:

USER INFO

DONATION INFO
DONATION INFO

SALES INFO
SALES INFO
SALES INFO
SALES INFO

LOTTERY INFO
LOTTERY INFO
LOTTERY INFO

FOOTER

Thanks in advance!


Solution

  • The answer could be late but i think it would help someone,

    If your JSON contains whole data and if you want to pass a set of particular data to sub report, Yes you can pass like below,

    Eg: Your JSON,

     {
      "User": {
        "PersonalInfo": {
          "Name": "Jef",
          "Country": "US"
        },
        "Donation": {
          "JustinWelfare": "10000",
          "DonationToWife": "50000"
        },
        "Sales": { "Some1": "some1" },
        "Lottery": { "Lot1": "lot1" }
      }
    }
    

    You can pass the Donation json to subreport like below,

    ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Donation")