Jasper Studio: JSON-Datasource with Sub-Arrays - json

I am using Jasper Studio 5.5 and try to create a report using a JSON-Datasource. My example json-file looks as followed:
{
"dto": [
{
"item": {
"active": 1
},
"itemProjects": [
{"id": 1},
{"id": 2}
]
},
{
"item": {
"active": 1
},
"itemProjects": [
{"id": 3},
{"id": 4}
]
}
]}
To summarize it: I have an array of "dto"-objects. Each dto contains an object "item" and an array of "itemProjects".
Using the Select-Query "dto" and the Fields "item.active" allows me to read the property "acttive". However I have yet to find a way to read the properties from the sub-arrays. I tried it with different Select-Strings and Fieldnames, but had no success. I also tried passing the array as parameter to a subreport, but this also didnt worked (the generated report showed empty fields).
Does anyone know, how to correctly read data from an array inside an array in JSON ?
best regards.

I take care my object structure in json, fits the report. Logically you receieve the complete json in the main report.
Then I make a sub-report and tell the subreport to select a part of the JSon only. As an example, my main report calls the subreport by using a Data Source Expression:
((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("data.bezirkResultatAbstimmungVorlage")
data.bezirkResultatAbstimmungVorlage then looks like this in the json:
"data":
{
"bezirkResultatAbstimmungVorlage":
[
{
"vorlageNummer": "4",
"offiziellerAbstimmungBezeichnung": "Ja für Mundart im Kindergarten", ... ... ...
Then I select the fields of this part of the json in the subreport. If you then put these fields in the detail-band jasper will loop automatically over all elements of your json-array.
The way you described should actually work. Please make sure you include the json as a datasource in the "Dataset and Query Dialog"

I would tackle this by creating subDatasets as you have two arrays that you want to iterate over.
In the detail band of the main report, add a list element with a new DataSet and give it JRDatasource Expression:
((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("dto")
Create the field: active, which points to item.active,
Add a second list element to the detail band to the right of the first one with a new DataSet, and give it this JRDatasource Expression:
((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("itemProjects")
Create the id field which points to id.
Now you should be able to get the item and item project list for each object.

Related

Expression issues (JSON) with power automate

I'm working on power automate and I have an issue because I've tried a lot expressions and none of them working.
I did a loop (foreach) and inside a message (compose) and there I tried to display "f7626790-0756-43bf-a757-a645a33b853d" from"client"
To do so, I've tried all those expressions:
item()?['client']
item()?['client']?[0]
item()['client']
Here my json file:
[
{
"gift": [
{
"client": "f7626790-0756-43bf-a757-a645a33b853d",
"details": [
{
"client_id": 10859085,
"type": "christmas",
"application_id": "6e6d758d-8e74-3ae3-ac84-50eb23ae65f"
}
]
}
]
}
]
Thank's in advance.
Based on the JSON you provided, you have two arrays. So the question for me is, which array do you want to iterate? Both or the inner one?
I'm going to assume both or if not, it won't matter if you only ever have one item for both of them, my answer will still retrieve the client property.
The Initialize Data step has your JSON.
The Initialize Client step is a blank string at this stage.
The For Each Outer Item expression is simply a link to the Data variable as initialized in the first step.
The For Each Gift expression is set as ... item()?['gift'] ... as you can work out, that will loop over each gift item.
Finally, I get the value of the client property using this expression ... item()['client']

Checking for Dict Elements within a List (from json response)

I currently query this API recursively, and for each instance which I send in a request, I want to check inside the returned json for whether there are any objects within the parameter "causes_virtual". If there is an existing entry inside there, such as in this case: "id": 5408600, I want to print('Contains Entry') but if there is nothing within, I want to print('No Entry'). How can I check causes_virtual for whether or not it contains any additional elements within the curly braces? Currently I am using the method of checking for an empty list:
Either: if len(api_response_article[0]['causes_virtual']) == 0: or if not api_response_article[0]['causes_virtual']:
However, neither of these methods seem to do me any good because it seems to only check for items as if it was a traditional list, but from my understanding, it is a dictionary within a list.
{
"additional_info": "",
"approved_at": null,
"approved_by_id": null,
"causes_virtual": [ # THIS SECTION
{
"id": 5408600,
"models": [
"Potatoes"
],
"principals": "Some irrelevant text",
"title": "We farm potatoes or something"
}
],
"created_at": "2021-03-03T01:13:04.477348+00:00",
"created_by_id": 1019500
}
I suppose what I am looking for is the right if statement for seeing if there is anything inside causes_virtual.
Any help or pointers would be greatly appreciated.

How can I get the value from JSON in logic apps

This is my source ( JSON)
{
"Price": {
"For": "840.040",
"From": "2.990"
},
"ArticleNumber": "71151004",
"ArticleNumberPartitionKey": "7115",
"ForStatus": "ACTIVE",
"id": "71151004",
}
My requirement is to get True in Logic apps( Condition) if the JSON file contains "ForStatus":"ACTIVE".
I tried with this command but not working.
json(base64ToString(triggerBody()?['Content']))?['ForStatus']?['ACTIVE']
According to your description, I don't know where your source(json) come from. I'm not sure if the expression json(base64ToString(triggerBody()?['Content'])) you provided can get the json data rather than get null value.
I assume the expression json(base64ToString(triggerBody()?['Content'])) can get the json data. To implement your requirement, you can use json(base64ToString(triggerBody()?['Content']))?['ForStatus'] is equal to ACTIVE, like below screenshot:
If the property ForStatus may not exist in your json data, you can use string(json(base64ToString(triggerBody()?['Content']))) contains "ForStatus":"ACTIVE", like below screenshot:
Please note, use "ForStatus":"ACTIVE"(remove blank space before "ACTIVE") after the "contains" because when you use json() method, it will remove the blank space.

How to extract property that is an object in keen.io

Using extractions api in keen.io I can't get back specific properties that are objects.
curl "https://api.keen.io/3.0/projects/PROJECT_ID/queries/extraction?api_key=READ_KEY&event_collection=COLLECTION_NAME&timeframe=this_7_days"
Gives me back all properties, let's say
{"result": [
{
"userId": 1,
"keen": {"timestamp": 'val', "created_at": 'val'},
"name":'val'
}
]}
But if I want to get just "userId" and "keen", the "keen" gets ignored.
curl "https://api.keen.io/3.0/projects/PROJECT_ID/queries/extraction?api_key=READ_KEY&event_collection=COLLECTION_NAME&timeframe=this_7_days&property_names=["userId","keen"]"
{"result": [{"userId": 1}...]}
I also noticed that I can get back specific properties from keen object if I specify:
property_names=["userId", "keen.timestamp"]
Result
{"result": [
{
"userId":"1",
"keen":{"timestamp":"val"}
}
]}
But I would like to get the whole object without specifying all properties. I have a top level property that is an object with many properties.
After contacting keen.io (very responsive and informative) I confirmed that retrieving just specified object with all its properties is not implemented at the moment and the only solution for now is to either get all of them or specify each property in the request (like i did in question above).
They will discuss adding this feature as it makes sense to have it working like that.

Ember-Data: How to get properties from nested JSON

I am getting JSON returned in this format:
{
"status": "success",
"data": {
"debtor": {
"debtor_id": 1301,
"key": value,
"key": value,
"key": value
}
}
}
Somehow, my RESTAdapter needs to provide my debtor model properties from "debtor" section of the JSON.
Currently, I am getting a successful call back from the server, but a console error saying that Ember cannot find a model for "status". I can't find in the Ember Model Guide how to deal with JSON that is nested like this?
So far, I have been able to do a few simple things like extending the RESTSerializer to accept "debtor_id" as the primaryKey, and also remove the pluralization of the GET URL request... but I can't find any clear guide to reach a deeply nested JSON property.
Extending the problem detail for clarity:
I need to somehow alter the default behavior of the Adapter/Serializer, because this JSON convention is being used for many purposes other than my Ember app.
My solution thus far:
With a friend we were able to dissect the "extract API" (thanks #lame_coder for pointing me to it)
we came up with a way to extend the serializer on a case-by-case basis, but not sure if it really an "Ember Approved" solution...
// app/serializers/debtor.js
export default DS.RESTSerializer.extend({
primaryKey: "debtor_id",
extract: function(store, type, payload, id, requestType) {
payload.data.debtor.id = payload.data.debtor.debtor_id;
return payload.data.debtor;
}
});
It seems that even though I was able to change my primaryKey for requesting data, Ember was still trying to use a hard coded ID to identify the correct record (rather than the debtor_id that I had set). So we just overwrote the extract method to force Ember to look for the correct primary key that I wanted.
Again, this works for me currently, but I have yet to see if this change will cause any problems moving forward....
I would still be looking for a different solution that might be more stable/reusable/future-proof/etc, if anyone has any insights?
From description of the problem it looks like that your model definition and JSON structure is not matching. You need to make it exactly same in order to get it mapped correctly by Serializer.
If you decide to change your REST API return statement would be something like, (I am using mock data)
//your Get method on service
public object Get()
{
return new {debtor= new { debtor_id=1301,key1=value1,key2=value2}};
}
The json that ember is expecting needs to look like this:
"debtor": {
"id": 1301,
"key": value,
"key": value,
"key": value
}
It sees the status as a model that it needs to load data for. The next problem is it needs to have "id" in there and not "debtor_id".
If you need to return several objects you would do this:
"debtors": [{
"id": 1301,
"key": value,
"key": value,
"key": value
},{
"id": 1302,
"key": value,
"key": value,
"key": value
}]
Make sense?