How to deal with JSON-List as JsonDataSource

2

Hello Everybody

I have an Web-Application where I read JSON from a request in Java. Then I put the JSON in a JsonDataSource (with the help of spring).

If the Json is one Object there is no Problem to display the Object. But if I have a list then I don't know how to use it.

This is the Json Example

{
    "project": {
        "version": 1,
        "id": 1,
        "name": "Project1"
    },
    "persons": [
        {
            "name": "Test Person 1",
            "favoriteColor": "red"
        },
        {
             "name": "Test Person 2",
            "favoriteColor": "blue"
        }
    ]
}

Java Code

InputStream stream = new ByteArrayInputStream(body.getBytes("UTF-8"));
JRjsonsource = new JsonDataSource(stream);
 
ModelAndView model = new ModelAndView(
                templatename + "/" + templatename + ".jrxml",
                createParameterMap(JRjsonsource));

JRXML

The project.name works

But how I have to define the persons? That I can use it in a list or in a Subreport

.....
<field name="projectName" class="java.lang.String">
     <fieldDescription><![CDATA[project.name]]></fieldDescription>
 </field>
 
 <field name="persons" class="net.sf.jasperreports.engine.data.JsonDataSource">
     <fieldDescription><![CDATA[persons]]]></fieldDescription>
 </field>
 
 
....
<subreport>
                <reportElement x="0" y="72" width="554" height="98" isRemoveLineWhenBlank="true" backcolor="#FFCC99" uuid="4eb7e220-6758-465f-83c9-45cdfecc33eb"/>
                <subreportParameter name="net.sf.jasperreports.engine.data.JsonDataSource">
                    <subreportParameterExpression><![CDATA[$F{persons}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression>
            </subreport>
 
 

Thanks for your help.

nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 10 months ago

Ok, for a Subreprot I found this:

http://community.jaspersoft.com/questions/803726/how-pass-json-list-subr...

But for a List?

nmau - 5 years 1 week ago

@Narcis in below soluation you have used subreportExpression 

<subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression> can you describe it what is jsonPerson.jasper

skumar_4 - 2 months 6 days ago

2 Answers:

10

Hi,

The following solution requires the jasperreports library to be at least version 5.0.4. 

Your jrxml fragment should look like this:

.....
<field name="projectName" class="java.lang.String">
     <fieldDescription><![CDATA[project.name]]></fieldDescription>
</field>
 
....
<subreport>
                <reportElement x="0" y="72" width="554" height="98" isRemoveLineWhenBlank="true" backcolor="#FFCC99" uuid="4eb7e220-6758-465f-83c9-45cdfecc33eb"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("persons")]]></dataSourceExpression>
                <subreportExpression><![CDATA["jsonPerson.jasper"]]></subreportExpression>
            </subreport>

and in your subreport you should discard the usage of the JsonDataSource parameter.

Hope this helps,

Narcis

narcism's picture
928
Joined: Nov 22 2010 - 12:39am
Last seen: 2 days 17 hours ago
0

Thanks a lot. This does the trick!

nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 10 months ago
Feedback
randomness