. How to import JSON data from REST API into BIRT
Keywords:Json data source Rest API data source
JSON data sources cannot be built in BIRT. Although some open source communities provide plug-ins for parsing JSON data sources, almost all plug-ins are very low-level and inconvenient to use, so they are not recommended.
The common way of BIRT is to create a "scripted data source", connect the scripted source to the URL and parse the result by itself, but the script is very cumbersome to write and needs a lot of work.
For example, to deal with such a scenario: Call the REST API according to the incoming parameters and return the nested JSON data. Some data formats are as follows:
[ { "id": 1000, "content": "It is too hot", "comment": [ { "author": "joe", "score": 3, "comment": "just so so!" }, { "author": "jimmy", "score": 5, "comment": "cool! good!" } ] } ... ] |
Expected effect after parsing:
BIRT uses the open() method to create a scripted data source, as follows:
... importPackage(Packages.java.io); importPackage(Packages.java.net); var param= params["industryname"].value; var inStream = new URL("http://yourapi/endpoint/" + param).openStream(); var inStreamReader = new InputStreamReader(inStream); var bufferedReader = new BufferedReader(inStreamReader); var line; var result = ""; while ((line = bufferedReader.readLine()) != null) result += line; inStream.close(); var json = JSON.parse(result); vars["HTMLJSON"] = json; logger.warning (result); ... |
It is recommended to use esProc, which encapsulates the return result of the URL string of HTTP service into a file stream, and can directly parse the JSON format string and structure it into a sequence table. esProc has designed a set of function library in the field of set operations to deal with all kinds of structured operations, such as grouping, sorting, filtering, aggregation, join, etc., without the help of a third-party database. For example, in the above problem, the script of esProc only needs three lines:
A |
|
1 |
=httpfile("http://yourapi/endpoint/servlet/testServlet?table=blog&type=json") |
2 |
=json(file(A1).read()) |
3 |
=A2.news(comment;id,content,${A2.comment.fname().concat@c()}) |
Finally, the calculation results are returned to the dataset of BIRT for report presentation. From this example we can see, compared with the conventional method, the implementation of the same algorithm requires less code, and step-by-step debugging is also convenient.
In fact, there are many similar calculation problems that are not convenient, but it is very simple with the help of esProc SPL. You can refer to: JSON data calculation and importing into database
esProc provides JDBC driver, and can be easily integrated with BIRT and other reporting tools. Please refer to How to Call an SPL Script in BIRT.
For esProc installation, free authorization and related technical information, please refer to Getting started with esProc
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Hi Andy,
My name is Jorge.
I am trying to reproduce your example, which has been really helpful.
I am using Birt 4.4.2, and I am using a Rest API URL (Start Wars) to test.
I am getting the error “JSON not defined”. I know that I am missing a library location[](http://).
Can you help?
Thanks ,
Jorge
Hi Jorge,
Do you use the js script in the BIRT native scripted data source for your test, or the esProc SPL script recommended in the article?
If you are using esProc SPL script, I will try my best to help you solve it.
Thanks.
Jerry
Hi Jerry,
This is the problem I am trying to solve:
I have used Birt for years to create reports with different data sources (SQL Server, text files, etc)
I have the need of creating Birt reports to access data in one of our systems using Rest APIs , and the datasets are returned in JSON format.
I have never used Birt with Rest APIs, and after searching internet for quite a while I found the approach of using scripted data sources. And then I found the RaqForum where you and Andy gave posted some examples.
Initially I am trying to create a simple test report using a scripted data source. I have not tried to use esProc SPL script yet. I plan to later. By the way, I didn’t know about the esProc SPL scripts.
But even with the simple report with the scripted data source I am getting the error “JSON not defined”. I have tried different options putting the json jar files in the scriptlib folder in my local Birt 4.8 installation. Still the same error.
Please advise.
Do you have have any sample Birt report using REST APIs that you can share ?
Thanks for getting back to me. Much appreciated.
Regards,
Jorge
I do not have the example of scripted data source for BIRT. There are multiple data sources in the old application, and a new data source has been added. For diversified data sources, esProc SPL can be solved in a consistent way. It is strongly recommended that you use new methods instead of cracking old methods.
Thanks for your feedback, appreciate it. I will explore the SPL script option.