Jump to content
We've recently updated our Privacy Statement, available here ×

Getting last element from array with jsonql


rogier_1
Go to solution Solved by narcism,

Recommended Posts

Hello,

I have a JSON datasource with an array of addresses. I need tp print the last element's street1 value. here's the json:

      "addresses": [          {              "id": 19764,              "addressType": {                  "name": "HOME",                  "i18nName": "address_type.HOME.NAME",                  "i18nDescription": "address_type.HOME.DESCRIPTION"              },              "street1": "Lage Naarderweg",              "houseNumber": 54,              "houseNumberSuffix": "",              "postalCode": "1211ac",              "city": "Hilversum",              "country": {                  "id": "NL",                  "countryCode3": "NLD",                  "name": "Netherlands",                  "countryCallingCode": 31              }          },          {              "id": 19765,              "addressType": {                  "name": "HOME",                  "i18nName": "address_type.HOME.NAME",                  "i18nDescription": "address_type.HOME.DESCRIPTION"              },              "street1": "Hoge Naarderweg",              "houseNumber": 54,              "houseNumberSuffix": "",              "postalCode": "1211ac",              "city": "Hilversum",              "country": {                  "id": "NL",                  "countryCode3": "NLD",                  "name": "Netherlands",                  "countryCallingCode": 31              }          }      ],

In my field I use this syntax:

 

<field name="street1a" class="java.lang.String">  <property name="net.sf.jasperreports.jsonql.field.expression" value="person.addresses.*[-1].street1"/></field>

If I replace -1 with 0, I get the first element's street1 value: Lage Naarderweg, which is correct

If I replace -1 with 1, I get the 2nd (= last)  element's street1 value: Hoge Naarderweg, which is correct

But -1 gives me null.

According to this doc: http://jasperreports.sourceforge.net/sample.reference/jsonqldatasource/index.html, this syntax should work.

 

Anyone any ideas?

 

Rogier

 

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution

The only place you could use negative indexes is inside an "Array Slice" expression. In your case that would mean having:

<field name="street1a" class="java.lang.String">  <property name="net.sf.jasperreports.jsonql.field.expression" value="person.addresses.*[-1:].street1"/></field>[/code]

 

Link to comment
Share on other sites

  • 1 year later...

That's my code (I'am using JasperReports 6.17.0.)

I would like to fetch first legalName, I tried upper code and doesn't work for me, I'am still getting a whole list.

Any idea would be welcome,

Thanks in advance

 

.......

<subDataset name="personalRef" uuid="a6159040-05bf-49fe-baf1-4aa9bf09dce1">
        <property name="com.jaspersoft.studio.data.defaultdataadapter"/>
        <queryString language="jsonql">
            <![CDATA[]]>
        </queryString>
        <field name="legalName" class="java.lang.String">
            <fieldDescription><![CDATA[legalName]]></fieldDescription>
        </field>
    </subDataset>

........

 

.........

<componentElement>
                <reportElement x="87" y="590" width="383" height="39" uuid="331a47a1-752a-4753-8250-6eb82ee51545">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                </reportElement>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="personalRef" uuid="de4ba941-86a9-421d-bb77-992f7b046c9c">
                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("personalReferences")]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="39" width="383">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="75663e58-778e-4f46-a05f-1c8208d32d16"/>
                            <textFieldExpression><![CDATA[$F{legalName}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>

 

.......................

 

Ex:

{
    "customerNumber": 209548,    
    "customerCity": "Holland City",
    "customerState": "OH",    
    "personalReferences": [{
        "legalName": "COLL MEJIA",        
        "city": "11",
        "state": "5",
        "zipCode": "22",        
    },
    {
        "legalName": "PAUL HOUGHLON",
        "city": "12",
        "state": "4",
        "zipCode": "44",
        }]
}

 

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