0 Replies Latest reply on Mar 28, 2016 10:17 PM by Kurt Fredericks

    getDataAsync() has arrived: Retrieve JSON output from Tableau's JavaScript API!

    Kurt Fredericks

      Hi guys,

       

      I've been retrieving JSON from Tableau's JavaScript API for some time now, and I've decided to package up how I've been doing it, and share it in a github library:

       

      https://github.com/kfred/getDataAsync

       

      The getDataAsync() function is an asynchronous function which creates a promise to complete the request to Tableau and once that promise is fulfilled, returns the data from Tableau.

       

      Once you attach the getDataAsync.js build to your Web application, you can use it as such:

       

      var sheet = viz.getWorkbook().getActiveSheet();
      
      getDataAsync(sheet, "Gender", "Male").then(function (data) {
        console.log(data.length);
      });
      

       

      In the above example, "Gender" is a filter, "Male" is the filter value, and data is the array of objects that Tableau Server returns from your request.

       

      As you'll see in my github project, there is an optional fourth argument that can be specified with options.

       

      Update: 3/15/16

      Right now the options argument allows you to specify four different settings, each of which can make certain tasks easier. Please review the readme file on the github page for definitions of each of those settings. Below are the default options:

       

      var options = {
        "clean": false,
        "flatten": false,
        "raw": false,
        "filter": true
      };
      

       

      Update: 3/28/16

      You can now easily handle errors from Tableau using the .catch() pattern on the getDataAsync function:

      var sheet = viz.getWorkbook().getActiveSheet();
      
      getDataAsync(sheet, "Gender", "ale")
      .then(function (data) {
           //do something with JSON
           console.log(data.length);
      })
      .catch(function (err) {
           //do something on error
           alert(err);
      });
      

       

      I know Tableau has stated that getDataAsync() will be offered natively in the JavaScript API at some point in the future. But for now, until then, you have a solution!

       

      There are really cool things you can do with this data--d3, in particular. Let me know if you have any questions or suggestions on additional features I should include. I will be actively supporting this project, and am open to accepting pull requests for your own contributions.

       

      I hope you enjoy it!

      Kurt

       

      Message was edited by: Kurt Fredericks