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

xpath // with json?


nicknxswart.nl
Go to solution Solved by narcism,

Recommended Posts

Hi!

I switched from datasource from xml to JSON.

Before I did //name to get the field from the datasource because I do not know the parants of the object.

I want do do the same thing with JSON so I ried ..name and $..name and so on but I cant seem to get it to work.

Any tips?

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

So say that you have a json:

{
        "customer": {
            "code": "102",
            "name": "Jan"
        }
}

What if you want to get the "name" from the JSON to display in a report.

Normaly in the dataset and query I would set the query path to customer.

But say for example the json looks like:

{
    "something": {
        "customer": {
            "code": "102",
            "name": "Jan"
        }
    }
}

I want to fetch the field name to display in the report.

I could set the dataset and query to "something" and the field expression to: customer.name.

Than it will work but what if I don't know and don't care about the parant.

Normaly in xpath I would do //name and it will fetch the field name from the xml.

But now in json I cant get it to work to in the field expression search for the field name.

Link to comment
Share on other sites

  • Solution

You need to set the query language to JSONQL/jsonql for "..name" or "$..name" to work.

This also needs to be matched with the proper field expression like so:

<queryString language="jsonql">    <![CDATA[..name]]></queryString><field name="name" class="java.lang.String">    <property name="net.sf.jasperreports.jsonql.field.expression" value="[0]"/>    <fieldDescription><![CDATA[The name]]></fieldDescription></field>[/code]

This is a special case where the query returns only the leaf nodes of the JSON tree, hence the weird expression. However, if you had "..customer" as your main query, you could use <property name="net.sf.jasperreports.jsonql.field.expression" value="name"/> for your field.

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