Facility Control

 View Only
Expand all | Collapse all

Dashboard - Call Data from online JSON and send to local XML

  • 1.  Dashboard - Call Data from online JSON and send to local XML

    Posted 10-16-2019 20:18

    Hello.  Looked all over the forums but couldn't really find anything that would solve this.  I'd like to be able to pull some data fields out of an online stats feed that is JSON formatted and send those specific fields into their own local XML file.  For example:  player name, points, goals, assists, power play goals, shorthanded goals, penalty minutes, plus/minus.  There are a bunch of other fields in the feed that I do not need.  

    I don't want to set up a datalinq for each team and stat category (there would be 16 teams times each stat category).  I'd like for my operators to just be able to press a button in Dashboard that would access that teams' player stats and then assign to an "AwayStats.xml" which will then populate within XPression.  The feeds come up as "Sitekite/Statviewtype" then "Sitekit/Statviewtype<2>, etc ... with the row being the category I'm displaying.

    I know Dashboard can access online data and I know Dashboard can write specified data to a local file, I just don't know how to do it together.  I have literally no coding experience.  Any help would be greatly appreciated!



  • 2.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 10-17-2019 09:40

    I get where you're comming from... But why would you need 16 tables? All player could be in one table as long as you give each team a unique ID that can then be used to tell what team each player is part of.


    #DashBoard


  • 3.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 10-17-2019 13:39

    Hey thanks!  That would work as I can pull up all the players in the league, but I can't figure out the scripting to just pull out the specific players by their team id number, so I get a return of all 716 players with all of their stats.  The team IDs are not consecutive, so one team may be 319, the next 406, etc.  How would I pull just a specific team id?

     

     


    #DashBoard


  • 4.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 10-17-2019 20:17

    Hi Brandon,

    Here are examples attached  xml files to show how DashBoard to read Json files and how to create XML locally from DashBoard.

    1)Json example:

    Open “Json to Struc.grid”  file on DashBoard and type json file name “sample-data.json”

    note: rename "Json to Struc.xml" to "Json to Struc.grid"

    "Json to Struc.xml"

    <?xml version="1.0" encoding="UTF-8"?><abs contexttype="opengear" id="_top">
    <meta>
    <params>
    <param access="1" maxlength="0" name="Data URL" oid="Data_URL" stateless="true" type="STRING" value="" widget="text"/>
    <param access="1" name="Structured Data" oid="struct-data" stateless="true" type="STRUCT_ARRAY" widget="table">
    <value>
    <subparam access="1" maxlength="0" name="name" suboid="team" type="STRING" value="" widget="default"/>
    </value>
    </param>
    </params>
    </meta>
    <param expand="true" height="61" left="18" oid="Data_URL" right="17" top="20">
    <task tasktype="ogscript">function callback(resultStr)
    {
    if (resultStr != null)
    {
    var json = JSON.parse(resultStr);
    var a = null;
    if (Array.isArray(json))
    {
    a = json;
    }
    else
    {
    a = [];
    a.push(json);
    }
    params.setAllValues('struct-data', a);
    }
    }
    params.resetAllValues('struct-data');
    ogscript.asyncPost(params.getValue('Data_URL', 0), null, callback);</task>
    </param>
    <param bottom="10" expand="true" left="19" oid="struct-data" right="9" showlabel="false" top="94"/>
    </abs>

    ==================

    sample-data2.json as below

    [{
    "name":"John",
    "age":30,
    "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
    }
    },
    {
    "name":"John",
    "age":30,
    "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
    }
    },
    {
    "name":"John",
    "age":30,
    "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
    }
    },
    {
    "name":"John",
    "age":30,
    "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
    }
    }]

     

    2) Create xml file example

    Open Create XML Document.grid on DashBoard,  click “Write XML”  button, game-data.xml file is created.

    note: rename "Create XML Document.xml" to Create XML Document.grid

    Create XML Document.xml file as below

    <?xml version="1.0" encoding="UTF-8"?><abs contexttype="opengear" style="">
    <button buttontype="push" height="104" left="17" name="Write XML" top="18" width="160">
    <task tasktype="ogscript">var xmlDoc = ogscript.parseXML('&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;game/&gt;');
    var gameElement = xmlDoc.getDocumentElement();

    var homeTeam = xmlDoc.createElement('home');
    homeTeam.setAttribute('short', 'GG');
    homeTeam.setAttribute('long', 'Good Guys');
    homeTeam.setTextContent('Some Text Here');
    gameElement.appendChild(homeTeam);

    var awayTeam = xmlDoc.createElement('away');
    awayTeam.setAttribute('short', 'BG');
    awayTeam.setAttribute('long', 'Bad Guys');
    awayTeam.setTextContent('Some More Text Here');
    gameElement.appendChild(awayTeam);

    ogscript.saveToFile('game-data.xml', gameElement, true);</task>
    </button>
    </abs>

    Hope it helps.

    Thanks,

    Michelle

     

     

     


    #DashBoard


  • 5.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 10-18-2019 14:06

    Thanks Michelle... I'm really not a programmer so I'm having trouble getting my head around how this actually works.  I took the code and placed it in Dashboard and got the layout, but when I paste in the actual web address with the data, nothing really happens.  What am I missing?  Screenshot of results attached...

     


    #DashBoard


  • 6.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 10-18-2019 17:56

    Hi Brandon,

    I took at look at the json file that is returned from the url that you specified.    It has multiple levels of data, and the example Michelle gave you deals with a fairly flat json file.

    If you want to parse something more complex, you will need to learn more about how json.parse works.    The code does parse all the data it received into a json object with the line:

    var json = JSON.parse(resultStr);

    There are tutorials on using JSON.parse online, it is a standard Javascript function.   See something like:

    https://www.w3schools.com/js/js_json_parse.asp

     

    Once you have the data in that object, you can do many things with the sub-data.   You should be able to access the actual data with something like:

    var data = json.SiteKey. Statviewtype

    Then iterate over all it’s elements.   There’s an example of iterating at:

    https://stackoverflow.com/questions/1637334/iterating-through-parsing-json-object-via-javascript

     

    Hopefully this gets you further.


    #DashBoard


  • 7.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 11-18-2019 20:18

    Did you make any progress with this? I am trying to do the exact same thing, hockey stats via LeagueStat, any would love to work on this with you if you are still working on it.


    #DashBoard


  • 8.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 07-28-2022 16:49
    Hi,

    I realize this is a few years old, but it is exactly what I am looking at right now. Online JSON feed from that same site/structure: https://lscluster.hockeytech.com/feed/?feed=modulekit&view=statviewtype&type=skaters&key=41b145a848f4bd67&fmt=json&client_code=whl&lang=en&season_id=275&team_id=212&league_code=&fmt=json&sort=active&order_direction=

    Trying to figure out how I can parse that and add some (but not all) of that data into Dashboard for use.

    Either of you guys ever make any progress with this?

    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------



  • 9.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 07-31-2022 20:01
    Hi there!

    Yes I made some major progress to this, and have a working dashboard, that can pull data from a whole host of players from different leagues and use that data for any graphics or data in XPression...

    It is a rather large project though, but if you can narrow it down to what you're looking for first, I'll see if I can answer.

    ------------------------------
    Aleksander Stalsberg
    Inland Norway University of Applied Sciences/Lillehammer Icehockey Club
    Norway
    ------------------------------



  • 10.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 08-02-2022 11:13
    Our hockey project is also quite large, but with the season coming up I've just been starting to look briefly at what I could do to improve/better things. One of the things that immediately came to mind was that it seems like a shame that our operator has to manually update roster spreadsheets before each game, when we are connected to the internet and it is all there just for the taking (if I knew how). I didn't build the original projects and haven't dived to deep into how they are built, so no guarantee I'd even be able to wrap my head around changing it off pulling data locally from spreadsheets to pulling it from the internet..... but the first step would be to see if it was possible to grab it from the net and how to do it.

    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------



  • 11.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 08-03-2022 09:11

    This is definetly possible, but how complex it ends up being depends on what you plan to use it for.

    In my case, I have a full table of the teams info, and with that list I have the name, the short version, the logo-path, the primary and secondary color and team statistics as well. But most importantly, a team ID (acting as primary key if you know database logic).

    Using that, I then have another table for each player, their name, pic, number, stats, so on... And also what team they play on (by id). Pulling data from a player also gives me the info of what team they are on, and pulling up a team, I can make a list of each player on that team, so on so forth...

    So based on that, I pull the teams from the league with JSON, populate that table first. Then I have another button that lets me pull the individual player data from all players on the selected team, and also for todays match. And each of these players also have their own player ID.
    Takes some planning to set it up in a way that you dont end up with a ton of redundant data and/or alot of manual entry if not everything is pulled from the JSON data as well...

    I dont have time right now to give you an example, but parsing the JSON data is fairly simple.
    Look here for some examples: https://www.w3schools.com/js/js_json_parse.asp



    ------------------------------
    Aleksander Stalsberg
    Inland Norway University of Applied Sciences/Lillehammer Icehockey Club
    Norway
    ------------------------------



  • 12.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 08-04-2022 16:31
    I think I have figured it out. Might not be the neatest code, but it proves the concept of what I need to do. I will share what I have done here for future knowledge seekers:

    Looking at the URL: https://lscluster.hockeytech.com/feed/?feed=modulekit&view=statviewtype&type=skaters&key=41b145a848f4bd67&fmt=json&client_code=whl&lang=en&season_id=275&team_id=212&league_code=&fmt=json&sort=active&order_direction=

    Code that takes three items from there, could be expanded on to take more, but this gives the basic idea:

    function callback(resultStr)
    {
    var number;
    var name;
    var position;
    
    var numberArray = new Array();
    var nameArray = new Array();
    var positionArray = new Array();
    
      if (resultStr != null)
      {
         var json = JSON.parse(resultStr);
         for (var n=0;n<50;n++)
    {
       number = json.SiteKit.Statviewtype[n].jersey_number;
       name = json.SiteKit.Statviewtype[n].name;
       position = json.SiteKit.Statviewtype[n].position;
    
       if (name != n)
          {
             ogscript.debug('Adding Entry: ' + number);
             ogscript.debug('Adding Entry: ' + name);
             ogscript.debug('Adding Entry: ' + position);
             numberArray.push(number); 
             nameArray.push(name); 
             positionArray.push(position);     
          }
        params.setAllValues('Home.Numbers', numberArray);
        params.setAllValues('Home.Names', nameArray);
        params.setAllValues('Home.Positions', positionArray);
    }
    
     }
    }
    var URL = params.getValue('URL', 0);
    ogscript.asyncPost(URL, null, callback);​





    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------



  • 13.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 08-05-2022 08:19

    That looks perfect to me!

    Now I would recommend looking into Struct Tables in order to store the data.
    From there you can put all of this data into an organized table, and then call the info from that table into whatever information you need to populate individual Xpression Graphics for example.



    ------------------------------
    Aleksander Stalsberg
    Inland Norway University of Applied Sciences/Lillehammer Icehockey Club
    Norway
    ------------------------------



  • 14.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 09-01-2022 13:17
    So, I've got the Struct Table working perfectly for storing home and away team rosters. Just looking at doing the same thing to pull standings now... cause why not, I thought it would be easy enough. However, for whatever reason I can't duplicate my results with it. It is weird. Code is below:

    function callback(resultStr)
    {
      if (resultStr != null)
      {
         var json = JSON.parse(resultStr);
         for (var n=0;n<50;n++)
    {
       var rank = json.SiteKit.Statviewtype[n].rank;
       var name = json.SiteKit.Statviewtype[n].team_name;
       if (name != null)
          {
             ogscript.debug('Adding Entry: ' + rank);
             ogscript.debug('Adding Entry: ' + name);    
          
    var structCount = params.getElementCount('Conference_Standings');
    
    if (structCount == 1 && params.getValue('Conference_Standings.0.Rank', 0) == "") // If this is the first element and it has not been set yet
    {
    structCount = 0;
    }
    
    
    var newValue = {
       "Rank": rank,
       "Name": name,
       };
    
    
    ogscript.debug("newValue: " + newValue);
    params.setValue('Conference_Standings', structCount, newValue);
    
    
    }}
    
      
       }
       }
     
    
    
    var URL = params.getValue('Conference_StandingsURL', 0);
    ogscript.asyncPost(URL, null, callback);​


    The weird parts are:
    - With this current script I only see in the debug monitor, it adding the first entry of rank/name, but it doesn't add it to the table.
    - If I take away the "params.setValue('Conference_Standings', structCount, newValue);" line, I can see in the debug monitor that it adds all rank/name entries just fine.
    - If in setting the newValue I put something other than the rank/name variables in there, it sets the values just fine for the appropriate number of teams... just obviously not with the variable values that I want to be in there. (When I do this I see all the entries add in the debug monitor as well... so it seems like having the variable in the newValue is what breaks it... but that doesn't make sense because that's exactly what I'm doing for the roster ones that work fine)

    So not sure what I'm doing wrong on this one.



    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------



  • 15.  RE: Dashboard - Call Data from online JSON and send to local XML

    Posted 09-06-2022 12:52
    Went back to this after a few days. My issue was that the "rank" item in the JSON page wasn't formatted standard so I put in a line of code to convert it to a string first. I seem to solve my own problems half the time after posting on these forums.

    ------------------------------
    Jake Lew
    Access Communications Co-Operative Limited
    ------------------------------