Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a json array to a table using java so that I can pass cookies?

I have json object like this

{
  "data":[
    {
       "no":1,
       "name" : "yuda",
       "address" : "surabaya"
    },
    {
        "no":2,
        "name":"adi",
        "address":"jakarta"
    }
  ],
  "comp_name":"TEST CORP",
  "title":"Company employee",
  "status":"success"
}

I serve that json with http rest API and I want to create report with jasper report and I have success if I pass it as an url JsonQueryExecuterFactory.JSON_SOURCE.

enter image description here

enter image description here

My problem is that I can't include session cookie in JSON_SOURCE, the solution I'm trying is to create a http request in java code and then include it with JsonQueryExecuterFactory.JSON_INPUT_STREAM, but it's not working, it just shows blank page. Even though I update parameter from $P{net.sf.jasperreports.json.source} to $P{JSON_INPUT_STREAM} it doesn't work.

like image 368
yuda Avatar asked May 24 '17 03:05

yuda


People also ask

How do I create a JSON array in Java?

Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.

How to write a JSON array to a database using JDBC?

To write the contents of the a JSON array to a database using JDBC − Register the Driver class of the desired database using the registerDriver () method of the DriverManager class or, the forName () method of the class named Class.

How do I insert key-value pairs in a JSON array?

Insert the required key-value pairs using the put () method of the JSONObject class. Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add () method of the JSONArray class.

How to create a relational table from a JSON object?

To create a relational table from a JSON object, the database needs to know several pieces of information. First, it must know where to locate the JSON object. Second, it must know the uppermost level for producing rows.


Video Answer


1 Answers

Here is the way to do it in newer JasperReports version 6.3.1 with the http properties and JSONQL:

The JRXML:

<?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="JsonHttpDataAdapterReport" columnCount="3" pageWidth="595" pageHeight="842" columnWidth="171" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50" uuid="b85a2cf8-dd0c-4a31-804c-e7b217f9b42b">
    <property name="net.sf.jasperreports.data.adapter" value="JsonHttpDataAdapter.xml"/>
    <parameter name="CookieParam" class="java.lang.String" evaluationTime="Early">
        <property name="net.sf.jasperreports.http.data.header" value="Cookie"/>
        <defaultValueExpression><![CDATA["name1=val1;name2=val2"]]></defaultValueExpression>
    </parameter>
    <queryString language="jsonql">
        <![CDATA[data.*]]>
    </queryString>
    <field name="CompanyName" class="java.lang.String">
        <fieldDescription><![CDATA[$.comp_name]]></fieldDescription>
    </field>
    <field name="Title" class="java.lang.String">
        <fieldDescription><![CDATA[$.title]]></fieldDescription>
    </field>
    <field name="Number" class="java.lang.String">
        <fieldDescription><![CDATA[no]]></fieldDescription>
    </field>
    <field name="Name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <field name="Address" class="java.lang.String">
        <fieldDescription><![CDATA[address]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="280" height="30" uuid="6333da35-2a4b-4373-9838-2d9e71981516"/>
                <textElement>
                    <font size="20"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{CompanyName}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="300" height="30" uuid="4062fecd-290c-4790-8f92-868d4c58f054"/>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Title}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="31" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="80" height="30" backcolor="#A7E9FA" uuid="b66244aa-5ae0-4e24-9b4f-aafd7f5883cc"/>
                <box topPadding="5" leftPadding="10" bottomPadding="0" rightPadding="0"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Number]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="80" y="0" width="185" height="30" backcolor="#A7E9FA" uuid="965b8d2b-2baf-4842-be30-430ae395a8aa"/>
                <box topPadding="5" leftPadding="10" bottomPadding="0" rightPadding="0"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="265" y="0" width="255" height="30" backcolor="#A7E9FA" uuid="f56fe462-a90f-4228-a243-3222552f7e45"/>
                <box topPadding="5" leftPadding="10" bottomPadding="0" rightPadding="0"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Address]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="30" uuid="6b8a6731-19d4-44ee-b41f-c6977729d334"/>
                <box topPadding="5" leftPadding="10"/>
                <textFieldExpression><![CDATA[$F{Number}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="0" width="185" height="30" uuid="6e51e1eb-ff0e-4e2f-aa05-056060901aff"/>
                <box topPadding="5" leftPadding="10"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="265" y="0" width="255" height="30" uuid="b79f1a8e-627f-4952-b5eb-632d5db771eb"/>
                <box topPadding="5" leftPadding="10"/>
                <textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The JsonHttpDataAdapter.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<jsonDataAdapter class="net.sf.jasperreports.data.json.JsonDataAdapterImpl">
    <name>JSON Http Data Adapter</name>
    <dataFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:type="httpDataLocation">
        <method>GET</method>
        <url>http://domaindotsomething/data.json</url>
    </dataFile>
    <language>jsonql</language>
    <useConnection>true</useConnection>
    <timeZone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
              xsi:type="java:java.lang.String">Europe/Bucharest</timeZone>
    <locale xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
            xsi:type="java:java.lang.String">en_US</locale>
    <selectExpression></selectExpression>
</jsonDataAdapter>

And the output:

enter image description here

Edit: This should be regarded as a JasperReports v6.3.1 API solution only, until a newer version of JasperSoft Studio is released with proper support for header properties/parameters. Current latest version of JasperSoft Studio, v6.3.1 final, prevents reports with URL-based JSON data adapters from being previewed. Even rebuilding JasperSoft Studio from the 6.3.1 tag with the fix for the bug that prevented it from working, does not seem to allow this solution to fully work with report based http properties, but only with fully configured JSON data adapter.

like image 187
Narcis Avatar answered Sep 30 '22 13:09

Narcis