Home > front end >  How can I access the nested information of a JSON received in a string parameter?
How can I access the nested information of a JSON received in a string parameter?

Time:09-29

I have a JSON data with a following structure:

{
    "country": "London",
    "name": "Bookstore",
    "books": [
        {
            "title": "book1",
            "description": "Awsome book!"
        },
        {
            "title": "book2",
            "description": "good book"
        },
        {
             "title": "book3",
            "description": "good book"
        }
    ],
    "openDate": "05/04/2000"
}

I am receiving this JSON in a string parameter of my report <parameter name="books" />

I have tried several things, such as changing the data type of the input parameter, using lists, subdataset or subreports, but I have not been able to access the nested information of the JSON, for example:

"title": "book1",
"description": "Awsome book!"

Can you help me with any suggestion?

CodePudding user response:

You can use a subdataset that has a json query and a list that passes the JSON data as the JSON_INPUT_STREAM parameter.

Like this:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="account_statement" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a49bb24-5523-475c-a23b-7afd45c4ce2d">
    <subDataset name="listJson" uuid="28ab008a-4582-4fe9-9ff2-8a19cb354732">
        <queryString language="json">
            <![CDATA[books]]>
        </queryString>
        <field name="title" />
        <field name="description" />
    </subDataset>
    <parameter name="books" />
    <title>
        <band height="20">
            <componentElement>
                <reportElement x="0" y="0" width="250" height="20" uuid="a6cc346e-d517-431e-8860-d81cc4e1145f"/>
                <c:list xmlns:c="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="listJson" uuid="b8180534-701e-4f9a-ad5f-785d1a5fb74b">
                        <datasetParameter name="JSON_INPUT_STREAM">
                            <datasetParameterExpression><![CDATA[new ByteArrayInputStream($P{books}.getBytes("UTF-8"))]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                    <c:listContents height="14">
                        <frame>
                            <reportElement x="0" y="0" width="250" height="14" uuid="28b3ebcb-1bd3-422d-9468-54216206315d"/>
                            <textField>
                                <reportElement x="0" y="0" width="100" height="14" uuid="2fba17f5-4547-4f51-a12f-815c7f0589ca"/>
                                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
                            </textField>
                            <textField>
                                <reportElement x="100" y="0" width="150" height="14" uuid="2cf85417-758d-4c99-b552-64eca8a5dfef"/>
                                <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
                            </textField>
                        </frame>
                    </c:listContents>
                </c:list>
            </componentElement>
        </band>
    </title>
</jasperReport>
  • Related