Jump to content
Changes to the Jaspersoft community edition download ×

json parsing engine and query syntax for JsonDataSource


rlothai

Recommended Posts

 Hello,

I want to use the JsonDataSource in our bank project and have begun experimenting with it in iReport, with some success. Does anyone know if there is a third-party parsing engine being used or is this home grown? I could not glean the answer from a short look at the code. I need to locate a more or less complete reference for the query syntax, as I will be doing complex logic for some fields.

I can so far grab values from within my own JSON string using syntax like: sections[0].fields[0].value in my fielddescriptions. I really need to be able to look for specific values of the keys for objects in an array, but cannot figure out what that syntax should look like. Later I will also need to show or hide fields based on boolean values stored with the fields (are functions available, and how?).

I downloaded and got the JSON example working with the queryString syntax in the orders report: Northwind.Orders(CustomerID == $P{CustomerID} and was hoping to use something similar. So far I can't get the queryString to work with a similar lookup in my own json, and the == does not seem to work in fields either (not sure they are supposed to in fields, as not only can I not find documentation but I am also new to Jasper).

While I can give you example JSON and have you solve the problem, what I would prefer is a link to some decent documentation I can digest and use myself.

Thanks in advance and looking forward

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Hello, this is a bump with a more specific question. Assuming an object array with an embedded object array, like this:

"sections": [{

"id": "section-Id-One",
"property-One": true,
"fields": [{
"id": "field-Id-One",
"property-One": false,
"property-Two": true,
...
},
{
"id": "field-Id-Two",
"property-One": false,
"property-Two": true,
...
}
.....

I have succeeded in getting a query like this to work: 

sections(id ==  section-Id-One)
 
But I cannot go deeper into the object graph, like:
sections(id == sectionId).fields(id ==  field-Id-One)
Seems nested arrays are a challenge. But I have a structure I cannot change and need to get at the properties. Is this supposed to work?
 
Also, it would be great if someone just said, "oh, JSON queries in the datasource work like X or Y", or maybe "don't use this datasource because it's unfinished." Perhaps it's still too new, but I think it is an attractive option.
Link to comment
Share on other sites

  • 4 months later...

The solution:

sections(id == sectionId)[0].fields(id ==  field-Id-One)

The explanation:

the filtering expression  sections(id == sectionId) does not necessarily return ONE object, in fact it returns an array of objects, which contains only one element in this case. Using this one object in an array has the same effect in jasper then if it would be a single object.

For example, the array could contain all objects/sections which do not have this id: sections(id != sectionId)

If one tries to access the array with the "." operator, it will fail anyhow.

So if you know that the array only contains that ONE object, you can get it directly with "array[0]"

 

 

 

Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...

If I change the JSON to something like:

 

"sections": [{

"id": {

"myid" : "section-Id-One"

},

...

 

The Search doesnt seem to work anymore:

 

sections(id.myid == section-Id-One).fields(id == field-Id-One)

 

Does it support this kind of query in the JsonPath used in JasperReports as in XPath this is a standard lookup.

 

Link to comment
Share on other sites

  • 2 years later...

Is there a solution to get only 1 field : sections as a Json array with objets that are made of 3 attributes :

"id" (string), "property-One" (boolean) and fields (json array of json objects)

 

and then use expressions inside the report to show specific values

 

Best regards,

 

Guillaume

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