open JSON data from an url

Options
Martial Wilbert
edited February 11, 2022 in Analytics #1
<p>Hi,</p>
<p> </p>
<p>I need to create a report that using Json formatted data as source.</p>
<p>For my tests, I used a parameters called jSonData where I just copied a jSon Data sample.</p>
<p> </p>
<p>I was a able to retrieve all the data I needed and create the report design by using the eval() old school style.</p>
<p> </p>
<p>Now, in production, JSon data will come from a server. Means that I have to use a specific url. This url sent me back the Json Data as content.</p>
<p> </p>
<p>I thought that it will be simple, In fact, it's just stream an url content to a string and make an eval on this string.</p>
<p>But it seems that inputstream is not so easy on BIRT scripting cause I do not found how to do that.</p>
<p> </p>
<p> </p>
<p>Spend couple of hours to looking for a little piece of code that will save me :)</p>
<p>I found several nice information to use url for XML DataSource. </p>
<p> </p>
<p>Found that I may can use jsonparser but my "dear" customer explicitly explained that "no additionnal jar files can be deployed on his server".</p>
<p>Why doing simple when it can be complex ? :P  Enjoy !!</p>
<p> </p>
<p>Did someone can explain me how can I get the content from an URL to a string ?</p>
<p> </p>
<p>Thanks for your help.</p>
<p> </p>
<p> </p>

Comments

  • kclark
    kclark E
    edited September 9, 2014 #2
    Options
    <p>Try doing something like this</p>
    <pre class="_prettyXprint _lang-">
    importPackage(Packages.java.io);
    importPackage(Packages.java.lang);
    importPackage(Packages.java.net);

    url = new URL("http://localhost:8080/birt420/test.json");
    conn = url.openConnection();
    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    response = new StringBuilder();
    inputLine = new String();

    while ((inputLine = br.readLine()) != null)
    response.append(inputLine);
    br.close();

    response.toString(); // Eval this
    </pre>
    <p>I stopped at the eval but this should work for you :)</p>
    <p> </p>
    <p>Edit:</p>
    <p>FWIW I've been told that using eval is insecure even though I haven't looked into it much.  I know your client doesn't want to use additional jars but it's worth bringing that point up to him. there are a few JSON parsing lib's out there that made my life a whole lot easier when using them over eval.</p>
    Warning No formatter is installed for the format ipb
  • <p>Thanks Kristopher.</p>
    <p>Seems not able to use your solution.</p>
    <p> </p>
    <p>What I've done to be as clear as possible: (quite difficult for a french guy  :P)</p>
    <p> </p>
    <p><strong><u>Initial state:</u></strong></p>
    <p>one parameter where I put my JSON formatted data.</p>
    <p> </p>
    <p>On initalize of my report, I put this code:</p>
    <pre class="_prettyXprint">
    jsonObject = eval( '(' + params["jsondata"].value + ')' ) ;</pre>
    <p>On fetch of my Dataset:</p>
    <div>
    <pre class="_prettyXprint">
    if (cnt < jsonObject.length)
    {
    row["Symbole"] = jsonObject[cnt].Symb;
    row["TempMin"] = jsonObject[cnt].TempMin;
    row["TempMax"] = jsonObject[cnt].TempMax;
    row["Label"] = jsonObject[cnt].fr;
    row["Date"] = jsonObject[cnt].DateFromXML;


    cnt++;
    return (true);
    }


    return (false);</pre>
    <p>Preview of my dataset is correct.</p>
    <p> </p>
    <p> </p>
    <p><u><strong>Integrating your code:</strong></u></p>
    <p>As for climbing stairs, best way is up step by step. :P</p>
    <p>I decide then to test first that I can read content from the url.</p>
    <p>I commented all the script lines for the data set.<br>
    I create another parameter called jsonurl.</p>
    <p>Use your code and just modified the last line:</p>
    <pre class="_prettyXprint">
    params["jsonurl"]=response.toString();</pre>
    <p>Then display the parameters on the layout.</p>
    <p>One step more cause the parameter include the JSON from the url. Champagne were exit from the fridge ready to be open and drink  :rolleyes: ...</p>
    <p> </p>
    <p><strong>Last step:</strong></p>
    <p> </p>
    <p>Uncomment my dataset script to have same script then initial state.</p>
    <p> </p>
    <p>Modify the code on initialize by addind this simple line:</p>
    <p> </p>
    jsonObject = eval( '(' + params["jsonurl"].value + ')' ) ;
    <p>Dataset preview returns 0 line...  :huh:</p>
    <p>Preview of the report:</p>
    <p>The parameters is correctly completed but I have the following error message:</p>
    <p> </p>
    <div>
    <div>
    <div>
    <pre class="_prettyXprint">
    "Java class "java.lang.Object" has no public instance field or method named "date". (/report/method[@name=&quot;initialize"]#18(eval)#1) "</pre>
    </div>
    <br><p> </p>
    </div>
    </div>
    <p>I suspected than may the "date" word on Json data create conflict. I just modified the code by:</p>
    <pre class="_prettyXprint">
    params["jsonurl"]=response.toString().replace("date","etrange");</pre>
    <p>Same error. Champagne gone back to the fridge  :angry:</p>
    <p>I will try other tests on my side. If you have any idea, let me know.</p>
    <p> </p>
    <p> </p>
    <p> </p>
    </div>
    <p> </p>
  • <p>Thanks for the update, can you post your rptdesign?  In the meantime, one of my colleagues emailed me this JSON example that might be helpful.  It doesn't use an additional jars but dose include on JS file.</p>
    Warning No formatter is installed for the format ipb
  • <p>Hi Kristopher,</p>
    <p>Sorry for the delay, was on my best friend wedding. You know now why I can't think JSON yesterday :)<br><br>
    I had a look on your collegue report specially the URLFetch. Sorry, but I don't understand how did you link the report to the js file.<br><br>
    Sorry about that ...</p>
    <p>Martial</p>
  • <p>The JavaScript library is linked via the Report's <em>Properties > Resources > JavaScript Files</em>.</p>
    <p> </p>
    <p>For the URLFetch example, there is one file listed under JavaScript Files -- json.js.</p>
    Warning No formatter is installed for the format ipb
  • Martial Wilbert
    edited October 28, 2014 #7
    Options
    <p>Well,</p>
    <p>I just tried to run the URLfetch example but it does not run properly.</p>
    <p>May I make something wrong or my BIRT Designer is not properly configured:</p>
    <p> </p>
    <p> </p>
    <div>The following items have errors:</div>
    <p> </p>
    <div>
    <div>ReportDesign (id = 1):</div>
    <div><span>+ </span>There are errors evaluating script "importPackage(Packages.java.io);<br>
    importPackage(Packages.java.net);<br><br>
    var inStream = new URL("<a data-ipb='nomediaparse' href='http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true'>http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true").openStream();</a><br&gt;
    var inStreamReader = new InputStreamReader(inStream);<br>
    var bufferedReader = new BufferedReader(inStreamReader);<br>
    var line;<br>
    var result = "";<br>
    while ((line = bufferedReader.readLine()) != null)<br>
    result += line;<br>
    inStream.close();<br><br>
    var json = JSON.parse(result);<br><br>
    vars["HTMLJSON"] = json;<br>
    ":<br>
    Wrapped java.net.ConnectException: Connection refused: connect (/report/method[@name=&quot;beforeFactory"]#4) birt.core.JavascriptCommonError ( 1 time(s) )<br>
    detail : org.eclipse.birt.report.engine.api.EngineException: There are errors evaluating script "importPackage(Packages.java.io);<br>
    importPackage(Packages.java.net);</div>
    <div><br>
                        </div>
    </div>
  • <p>It looks like the sample Google JSON URL doesn't provide the data in the format when the sample was first created.</p>
    <p> </p>
    <p>To fix this, in the beforeFactory event, change the URL and remove the "&futureevents=true" option.</p>
    <p> </p>
    <p>From:</p>
    <pre class="_prettyXprint">
    var inStream = new URL("http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true").openStream();</pre>
    <p>
     </p>
    <p>To:</p>
    <pre class="_prettyXprint">
    var inStream = new URL("http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending").openStream();</pre>
    Warning No formatter is installed for the format ipb
  • <p>Clement,</p>
    <p>Still have an error. I'm not sure that it's link to the JSON format. It's seems to me like the java is not really executed.<br><br>
    I supposed that the report works on your side ?  May I have a problem on my BIRT designer configuration ?<br>
    Then I tried to load your report on an apache server where I'm sure that the configuration is correct. But still the error messages... </p>
    <p> </p>
    <p> :blink:  :wacko:</p>
  • <p>Clément,</p>
    <p>Finally your report with the change of the Sample google URL working fine.</p>
    <p>I discover that my lovely company network have some filter on the firewall that prevent it to run. Outside this network, your report runs.</p>
    <p> </p>
    <p>Regards,</p>
    <p> </p>
    <p>Martial</p>
  • <p>Hi all,</p>
    <p>Just to give you a last news. </p>
    <p>I use the Clement source code, adapt it to my JSON flow, managing the proxy settings and everything working well.<br>
    Then, we have a solution to use JSON data :)</p>
    <p> </p>
    <p>Thanks for your help.</p>
    <p> </p>
    <p>Martial</p>
  • malaythecool
    edited September 22, 2015 #12
    Options
    <p>hi martial i have same problem and new to BIRT</p>
    <p> </p>
    <p>can you help me out with your code?</p>
  • <p>Hi malythecool,<br>
    How can I help you ?</p>
    <p>Martial</p>
  • <p>hi martial</p>
    <p> </p>
    <p>thanks for your reply</p>
    <p> </p>
    <p>i am using api to get json data like localhost:8080/REST/ABCAPI/name   from Rest call in application</p>
    <p> </p>
    <p>i get data likethis format [{"entityId":"474","RegCategory":"M","Regno":"ex"}]</p>
    <p> </p>
    <p>so i want to ask which is bestway to consume that JSON format data</p>
    <p> </p>
    <p>as i am new i am not able to do more things i tried via JDBC and worked well from calling it via JAVA class</p>
    <p> </p>
    <p>than same i want to do via REST</p>
    <p> </p>
    <p> </p>
    <p>give your sugggestion</p>
    <p> </p>
    <p>Thanks in advance</p>
    <p> </p>
    <p>Malay</p>
  • <p>Malay,<br><br>
    Use the json.zip file provided by Kristopher one year ago. Inside it, you have a rptdesign called URLFetch.rptdesign.<br>
    On the BIRT Designer Outline, have a look on the scripts part.</p>
    <p> </p>
    <p>I'm starting from this rptdesign to design my rptdesign.<br>
    Of course you need to adapt it according your JSON file flow.<br><br>
    Relating my proxy problem, I have to add the following line code on the beforeFactory part:</p>
    <p> </p>
    <div>
    <pre class="_prettyXprint">
    Packages.java.lang.System.setProperty("http.proxyHost", "yourproxyurlwithouthttpprefix");
    Packages.java.lang.System.setProperty("http.proxyPort", "yourproxyport");</pre>
    </div>
    <div>Hope it's helping as a starting point. If you already tried something, please send us the problem you met.</div>
    <div> </div>
    <div>Martial</div>
  • <blockquote class="ipsBlockquote" data-author="wilbemar" data-cid="139205" data-time="1443011384">
    <div>
    <p> </p>
    <p>Malay,<br><br>
    Use the json.zip file provided by Kristopher one year ago. Inside it, you have a rptdesign called URLFetch.rptdesign.<br>
    On the BIRT Designer Outline, have a look on the scripts part.</p>
    <p> </p>
    <p>I'm starting from this rptdesign to design my rptdesign.<br>
    Of course you need to adapt it according your JSON file flow.<br><br>
    Relating my proxy problem, I have to add the following line code on the beforeFactory part:</p>
    <p> </p>
    <div>
    <pre class="_prettyXprint">
    Packages.java.lang.System.setProperty("http.proxyHost", "yourproxyurlwithouthttpprefix");
    Packages.java.lang.System.setProperty("http.proxyPort", "yourproxyport");</pre>
    </div>
    <div>Hope it's helping as a starting point. If you already tried something, please send us the problem you met.</div>
    <div> </div>
    <div>Martial</div>
    <p> </p>
    </div>
    </blockquote>
    <p> </p>
    <p>It's been a while since I've looked at that example and I'm sure it could use a face lift.  But wilbemar is right, you'll need to work the sample around your JSON structure.  For what it's worth, if you don't want to use JQuery or pure JavaScript then you should be able to import 3rd party libraries and use them in your report (e.g. Gson, Jackson, etc)</p>
    Warning No formatter is installed for the format ipb
  • <p>hey guys,</p>
    <p> </p>
    <p>thanks for your responses.</p>
    <p>I had done something meaningful from my json data to birt report.</p>
    <p> </p>
    <p>Best Regards,</p>
    <p>Malay</p>
  • Nithyasree Selvakumar
    Options
    :)
  • <p>Hi, i am starting with BIRT and need connect  JSON with birt. I using XML before but now need usign a data json, i have a rest service  that generate a structure json and need read this strcutured to create a report in birt. PLEASE help me.  thant</p>
  • Clement Wong
    Clement Wong E mod
    edited May 10, 2017 #20
    Options
    <p>Have you taken a look at this DevShare example?</p>
    <p><a data-ipb='nomediaparse' href='http://developer.actuate.com/community/forum/index.php?/files/file/1128-querying-elasticsearch-with-birt-using-a-scripted-data-set/'>http://developer.actuate.com/community/forum/index.php?/files/file/1128-querying-elasticsearch-with-birt-using-a-scripted-data-set/</a></p&gt;
    <p> </p>
    <p>It demonstrates using a scripted data source / data set to access a REST based source that returns JSON.</p>
    Warning No formatter is installed for the format ipb
  • <p>Sorry, but i dont undertand this example... please i need any more easy i am starting with eclipse birt i have a versiion 4.6 NEON.</p>
    <p> </p>
    <p>This links talk on elasticSearch, i used datasource with XML with out problems but i dnt know how does with json</p>
  • Clement Wong
    Options
    <p>Open Source BIRT does not have an out of the box REST connection, whereas commercial BIRT (as of iHub 16), it ships with a REST Data Source that allows you to easily retrieve JSON from your REST based data sources.</p>
    <p> </p>
    <p>With Open Source BIRT, you have the option to use a scripted data source and data set to connect.  The description of the DevShare describes the 3 events that were modified.</p>
    <p> </p>
    <p>First, you'll need to identify the REST URL.  In the <em>open </em>event of the scripted Data Source of the example, you'll see the REST based data source URL, and the we opening up a connection and retrieving the results in the "result" variable.   The scripted data set loops through the JSON results and populated the output columns.</p>
    <p> </p>
    <p>Although the DevShare example references ElasticSearch, it can be used for other REST based URL data sources.</p>
    Warning No formatter is installed for the format ipb