3

I am using Jasper Studio 5.5 and try to create a report using a JSON-Datasource. My example json-file looks as followed:

{
"dto": [
    {
      "item": {
            "active": 1
      },
       "itemProjects": [
            {"id": 1},
            {"id": 2}
        ]
    },
    {
        "item": {
            "active": 1
        },
        "itemProjects": [
            {"id": 3},
            {"id": 4}
        ]
    }
]}

To summarize it: I have an array of "dto"-objects. Each dto contains an object "item" and an array of "itemProjects". Using the Select-Query "dto" and the Fields "item.active" allows me to read the property "acttive". However I have yet to find a way to read the properties from the sub-arrays. I tried it with different Select-Strings and Fieldnames, but had no success. I also tried passing the array as parameter to a subreport, but this also didnt worked (the generated report showed empty fields).

Does anyone know, how to correctly read data from an array inside an array in JSON ?

best regards.

Xecu
  • 101
  • 1
  • 5
  • Did you manage to solve it? – endamaco Aug 04 '14 at 11:12
  • Related http://stackoverflow.com/questions/34603844/how-to-create-multiple-tables-in-jasper-report-using-json-as-a-datasource/ and http://stackoverflow.com/questions/39399255/jasper-subreport-showing-only-one-entry-from-the-json-data-source-when-embedded – Petter Friberg Sep 14 '16 at 22:14

2 Answers2

1

I take care my object structure in json, fits the report. Logically you receieve the complete json in the main report. Then I make a sub-report and tell the subreport to select a part of the JSon only. As an example, my main report calls the subreport by using a Data Source Expression:

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

data.bezirkResultatAbstimmungVorlage then looks like this in the json:

    "data": 
{
    "bezirkResultatAbstimmungVorlage": 
    [
        {
            "vorlageNummer": "4",
            "offiziellerAbstimmungBezeichnung": "Ja für Mundart im Kindergarten", ... ... ...

Then I select the fields of this part of the json in the subreport. If you then put these fields in the detail-band jasper will loop automatically over all elements of your json-array.

The way you described should actually work. Please make sure you include the json as a datasource in the "Dataset and Query Dialog"

Mike
  • 381
  • 1
  • 4
  • 12
  • Hi there. I have an error on this ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("data.bezirkResultatAbstimmungVorlage"). Saying "missing EOF at '.'" – Carriane Lastimoso Mar 28 '22 at 01:05
0

I would tackle this by creating subDatasets as you have two arrays that you want to iterate over.

In the detail band of the main report, add a list element with a new DataSet and give it JRDatasource Expression:

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

Create the field: active, which points to item.active,

Add a second list element to the detail band to the right of the first one with a new DataSet, and give it this JRDatasource Expression:

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

Create the id field which points to id.

Now you should be able to get the item and item project list for each object.

Jamshaid
  • 370
  • 2
  • 11
  • 40
Gelun
  • 169
  • 1
  • 2
  • 8