Facility Control

 View Only
  • 1.  Parsing a local JSON file in Dashboard to get values for parameters

    Posted 11-10-2019 22:04

    I have a json file on my computer with information in a key:value format. I would like Dahsboard to read the file to pull values from it and assign the values as parameters in Dashboard to be used later on. I'm having difficulty on coding the ogScript to do this. Any help is appreciated. 



  • 2.  RE: Parsing a local JSON file in Dashboard to get values for parameters

    Posted 11-11-2019 17:43

    You need to do an asyncPost feeding the URL of the file on your file system and then just feed the result to JSON.parse.

    An example of the script can be found in the following grid. 

    <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="248" 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"/>
    <label height="38" left="18" name="File URL: " style="txt-align:east" top="29" width="223"/>
    </abs>

    To test the grid you can feed a .json file with the following sample data:

    [{
    "name":"John",
    "age":30,
    "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
    }
    },
    {
    "name":"Dave",
    "age":25,
    "cars": {
    "car1":"Toyota",
    "car2":"Jeep"
    }
    },
    {
    "name":"Helen",
    "age":20,
    "cars": {
    "car1":"Honda"
    }
    },
    {
    "name":"Tyler",
    "age":34,
    "cars": {
    "car1":"Ford",
    "car2":"Toyota"
    }
    }]

    #DashBoard