12 Replies Latest reply on Dec 1, 2016 4:29 AM by Geraldine Zanolli

    Problems getting json Data

    jens.bruckmann

      Hi all,

       

      I was now able to get the simulator running and to reproduce the first sample.

       

      I know tried to start building my first own webdataconnector, but for any reason it looks like I a am getting a loop. ( I canceled my try to get extract data after 1254 min).

       

      As I am absolutley new about this I really hope you can help me identfy the issue I have with my javascript. The simulator and the html is working fine btw.

       

      Here is the script:

       

       

      (function () {
          var myConnector = tableau.makeConnector();
      
         // Init function for connector, called during every phase
        myConnector.init = function(initCallback) {
            tableau.authType = tableau.authTypeEnum.custom;   
        }
      
      
      myConnector.getSchema = function (schemaCallback) {
          var cols = [
              { id : "datetime", alias : "Messzeitpunkt", dataType : tableau.dataTypeEnum.datetime },
              { id : "value_raw", alias : "Wert der Abfrage", dataType : tableau.dataTypeEnum.float },
         
          ];
      
      
          var tableInfo = {
              id : "Capacity_Management",
              alias : "Needed Sensors for a propper Capacity Management Report",
              columns : cols
          };
      
      
          schemaCallback([tableInfo]);
      };
      
      
      myConnector.getData = function(table, doneCallback) {
          $.getJSON("https://***/historicdata.json?id=2836&avg=0&sdate=2016-11-20-00-00-00&edate=2016-11-21-00-00-00&username=tableau-srv&passhash=3619439130", function(resp) {
              var feat = resp.features,
                  tableData = [];
      
      
              // Iterate over the JSON object
              for (var i = 0, len = feat.length; i < len; i++) {
                  tableData.push({
                      "id": feat[i].id,
                      "datetime": feat[i].properties.datetime,
                      "value_raw": feat[i].properties.value_raw,
                     
                  });
              }
      
      
              table.appendRows(tableData);
              doneCallback();
          });
      };
      
      
          tableau.registerConnector(myConnector);
        $(document).ready(function () {
          $("#submitButton").click(function () {
              tableau.connectionName = "Capacity Management";
              tableau.submit();
          });
      });
      })();
      

       

      Any Idea what´s going wrong there?

        • 1. Re: Problems getting json Data
          jens.bruckmann

          I am just testing and have some more findings.

           

          First of all the json file does not have an ID. so I removed the ID tag and my datetime is the Identifier. When I run the debugger it looks like I am getting an answwr but for any reason it is not writing into the table and I really don´t know why

          • 2. Re: Problems getting json Data
            jens.bruckmann

            Hi all,

             

            I just used Chrome now as browser and the developer tools are telling me following:

             

            XMLHttpRequest cannot load https://*****/api/historicdata.json?id=2836&avg=0&…0-00-00&edate=2016-11-20-01-00-00&username=tableau-srv&passhash=3619439130. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.

             

             

            Any Idea?

            • 3. Re: Problems getting json Data
              jens.bruckmann

              Ok this point I was able  to solve myself, too.

               

              for everybody who is maybe facing the same issue.

               

              The server where the Data is stored must be allowed to get connection from javascript. we have added Access-Control-Allow-Origin to the registry. For more information please google with the keyword cors https://www.google.com/search?q=cors&oq=cors&aqs=chrome..69i57j0l5.630j0j7&sourceid=chrome&ie=UTF-8

               

               

              I now have the next issue:

               

              SCRIPT5007: Unable to get property 'length' of undefined or null reference

               

              this is refering to following line "  for (var i = 0, len = feat.length; i < len; i++) { "

               

              used by the sample script.

               

              So if you know something please tell me

              • 4. Re: Problems getting json Data
                Geraldine Zanolli

                Hi Jens,

                 

                This error means that your array 'feat' is empty so you can't return any length.

                 

                The problem seems to come from the answer that your API is sending back.

                 

                Can you check the structure of the answer that "https://***/historicdata.json?id=2836&avg=0&sdate=2016-11-20-00-00-00&edate=2016-11-21-00-00-00&username=tableau-srv&passhash=3619439130" is sending you back?

                 

                In the tutorial they use "resp.features" because feature is one of the element of the schema.

                 

                I personally use this console to test my API and see the structure of the answer: Cookies Disabled

                 

                I hope it helps

                • 5. Re: Problems getting json Data
                  jens.bruckmann

                  Hi Geraldine,

                   

                  first of all thanks a lot for your try to help me.

                  When I open the link I am getting following back

                   

                  {"prtg-version":"16.2.25.4710","treesize":60,"histdata":[{"datetime":"20.11.2016 00:00:26","datetime_raw":42693.9586426736,"value":"38 d","value_raw":3336565.0000,"coverage":"100 %","coverage_raw":10000},{"datetime":"20.11.2016 00:01:26","datetime_raw":42693.9593371296,"value":"38 d","value_raw":3336625.0000,"coverage":"100 %","coverage_raw":10000},{"datetime":"20.11.2016 00:02:26","datetime_raw":42693.9600315509,"value":"38 d","value_raw":3336685.0000,"coverage":"100 %","coverage_raw":10000},{"datetime":"20.11.2016 00:03:26","datetime_raw":42693.9607260069,"value":"38 d","value_raw":3336745.0000,"coverage":"100 %","coverage_raw":10000}

                   

                  As I am a noob prgramming javascript what does this mean resp.features

                   

                  and the output from the console is following

                  HTTP/1.1 200 OK

                  Access-Control-Allow-Origin:
                  *
                  Date:
                  Wed, 30 Nov 2016 10:40:30 GMT
                  Content-Length:
                  8878
                  X-XSS-Protection:
                  1; mode=block
                  Expires:
                  0
                  Connection:
                  keep-alive
                  Content-Type:
                  application/json; charset=UTF-8
                  Server:
                  PRTG/16.2.25.4710
                  Pragma:
                  public
                  X-Content-Type-Options:
                  nosniff
                  Cache-Control:
                  cache, must-revalidate

                   

                  { "prtg-version": "16.2.25.4710", "treesize": 60, "histdata": [ { "datetime": "20.11.2016 00:00:26", "datetime_raw": 42693.9586426736, "value": "38 d", "value_raw": 3336565, "coverage": "100 %", "coverage_raw": 10000 }, { "datetime": "20.11.2016 00:01:26", "datetime_raw": 42693.9593371296, "value": "38 d", "value_raw": 3336625, "coverage": "100 %", "coverage_raw": 10000 },
                  • 6. Re: Problems getting json Data
                    Geraldine Zanolli

                    Jens,

                     

                    You are welcome.

                     

                    So here instead of:

                    var feat = resp.features, 

                    You should try: var feat = resp.histdata

                     

                    resp is the answer from the API and features is part of the answer's structure. It is why here you should use histdata and not features because your envelope is different.

                     

                    Tell me if it is working.

                    • 7. Re: Problems getting json Data
                      Geraldine Zanolli

                      And instead of:

                      "datetime": feat[i].properties.datetime, 

                      "value_raw": feat[i].properties.value_raw, 

                       

                      You should try:

                      "datetime": feat[i].datetime, 

                      "value_raw": feat[i].value_raw, 

                      • 8. Re: Problems getting json Data
                        jens.bruckmann

                        Hi Geraldine,

                         

                        ok I am now getting no error message anymore But I do not see any data in the simulator. I simply ran the script in tableau and I got a count of the measures but for datetime it is telling me Null.

                         

                        I will continue now as I think this is a dateformat error which I hopefully can solve. I mean if you have an idea I am happy to hear but the most important step you already helped me with

                         

                        I really have to say thank you and to someone else you know who I am talking about

                        • 9. Re: Problems getting json Data
                          Geraldine Zanolli

                          Jens,

                           

                          Good news!

                           

                          Can you try:

                          { id : "datetime", alias : "Messzeitpunkt", dataType : tableau.dataTypeEnum.date }, 

                           

                          Otherwise yes, you should write a small function to convert the date format before sending it to Tableau.

                          • 10. Re: Problems getting json Data
                            jens.bruckmann

                            ok this does not help as the date format is not the one Tableau is expecting as it looks like 20.11.2016 00:00:01   as example.

                             

                            So I really have to write a small function.

                             

                            Thanks again

                            • 11. Re: Problems getting json Data
                              jens.bruckmann

                              OK I really have no idea about javascript programming....

                               

                              I thought the var datetime is already given and I can use but when I try to add

                               

                              var dat = new Date (Datetime);

                               

                              to get the string to a date is not working.

                               

                              I think the first problem I have is I really have no idea where I have to implement the code snippet (in which function I thought in get.Data but this does not work) or even is the code snippet is correct.

                              • 12. Re: Problems getting json Data
                                Geraldine Zanolli

                                Hi Jens,

                                 

                                So here I would change in getSchema:

                                { id : "datetime", alias : "Messzeitpunkt", dataType : tableau.dataTypeEnum.date }

                                Because your API is sending only the date and not the time.

                                 

                                After can you try to add:

                                 

                                var date = new Date(parseInt(feat[i].datetime));

                                var dateFinal =  (date.getMonth()) +"/"+date.getDate()+"/"+ date.getFullYear();

                                 

                                After:

                                 

                                for (var i = 0, len = feat.length; i < len; i++) { 

                                 

                                So after you have:

                                 

                                "datetime": dateFinal, 

                                 

                                I hope it will work, let me know