rogier_1 Posted March 30, 2021 Share Posted March 30, 2021 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 correctIf I replace -1 with 1, I get the 2nd (= last) element's street1 value: Hoge Naarderweg, which is correctBut -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 More sharing options...
Solution narcism Posted March 31, 2021 Solution Share Posted March 31, 2021 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 More sharing options...
rogier_1 Posted March 31, 2021 Author Share Posted March 31, 2021 That worked! Can you maybe tell me where I could have found this in the documentation? Maybe there are some more features I need to know of? Link to comment Share on other sites More sharing options...
narcism Posted March 31, 2021 Share Posted March 31, 2021 If you search for "Array Slice" in the doc that you mentioned, you will be pointed to the specific Member type column where you'll find an example with this exact expression.For now, that is the only place where we have detailed the jsonql query language. Link to comment Share on other sites More sharing options...
rogier_1 Posted March 31, 2021 Author Share Posted March 31, 2021 You are absolutely right. Actually, this part is where I found the solution of the -1. I just missed the colon. Sorry about that and thanks a lot for your help!! Link to comment Share on other sites More sharing options...
propoleopure Posted June 30, 2022 Share Posted June 30, 2022 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now