4 Replies Latest reply on Oct 10, 2019 1:51 AM by Ciara Brennan

    How to handle nested json in the javascript portion of a web data connector?

    James Allison

      The Williams College Museum of Art has been building a gitHub repository to share digital data related to our collection. One piece of that data is a json file that is an array of objects, one of which is a nested array. The data looks like this:

       

      {

       

          "Events": [

              {

                  "eventId": 1516,

                  "eventName": "Brahim El Guabli, ARAB 249, f19",

                  "facultyMember": "Brahim El Guabli",

                  "subjectAndCourse": " ARAB 249",

                  "subject": " ARAB ",

                  "courseNbr": "249",

                  "institution": "Williams",

                  "description": "Trauma and Memory in Maghrebi and Middle Eastern LiteraturesLiz intro at beginningEssaydi,  Neshat - women and body; trauma, body, powerMontoya, Gilbertson - imprisonment, constraints, disappearanceGilbertson, Muniz - warGiacometti reference - Iraq??",

                  "startDate": "2019-11-19T09:55:00",

                  "startYear": "2019",

                  "startMonth": "11",

                  "startDay": "19",

                  "dayOfTheWeek": "Tuesday",

                  "terms": null,

                  "relatedObjects": [

                      {

                          "objectID": 16904

                      },

                      {

                          "objectID": 15985

                      },

                      {

                          "objectID": 31386

                      },

                      {

                          "objectID": 31387

                      },

                      {

                          "objectID": 17405

                      },

                      {

                          "objectID": 18971

                      },

                      {

                          "objectID": 9025

                      },

                      {

                          "objectID": 18655

                      },

                      {

                          "objectID": 18656

                      },

                      {

                          "objectID": 18654

                      }

                  ],

                  "data_date": "2019-09-12"

              },

              {

                  "eventId": 1517,

                  "eventName": "Laura Martin, ENVI 101, f19",

                  "facultyMember": "Laura Martin",

                  "subjectAndCourse": " ENVI 101",

                  "subject": " ENVI ",

                  "courseNbr": "101",

      .... more objects follow

       

      The array field called relatedObjects is handled when we pull the json file directly into Tableau 2019 desktop, but when we use a web data connector based on the sample wdc provided by Tableau we get only the first related object. I have tried changing the schema entry for relatedObjects to an array of string, but that is not working for me. Can anyone suggest specific changes to the javascript of our connector (shown below) to make it pull in all of the objectIDs for a given event? 

       

       

      (function () {

       

          var myConnector = tableau.makeConnector();


          myConnector.getSchema = function (schemaCallback) {

              var cols = [{

                  id: "eventId",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "eventName",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "facultyMember",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "subjectAndCourse",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "subject",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "courseNbr",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "institution",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "description",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "startDate",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "startYear",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "startMonth",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "startDay",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "dayOfTheWeek",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "terms",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "relatedObjects",

                 dataType: tableau.dataTypeEnum.string}

              ];

         

              var tableSchema = {

                  id: "eventsFeed",

                  alias: "WCMA events",

                  columns: cols

              };

         

              schemaCallback([tableSchema]);

          };


          myConnector.getData = function(table, doneCallback) {

              $.getJSON("https://raw.githubusercontent.com/wcmaart/collection/master/Events-utf8.json", function(resp) {

                  var feat = resp.Events,

                      tableData = [];

         

                  // Iterate over the JSON object

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

                      tableData.push({

                          "eventId": feat[i].eventId,

                          "eventName": feat[i].eventName,

                          "facultyMember": feat[i].facultyMember,

                          "subjectAndCourse": feat[i].subjectAndCourse,

                          "subject": feat[i].subject,

                          "courseNbr": feat[i].courseNbr,

                          "institution": feat[i].institution,

                          "description": feat[i].description,

                          "startDate": feat[i].startDate,

                          "startYear": feat[i].startYear,

                          "startMonth": feat[i].startMonth,

                          "startDay": feat[i].startDay,

                          "dayOfTheWeek": feat[i].dayOfTheWeek,

                          "relatedObjects": feat[i].relatedObjects.objectID

                         


                      });

                  }

         

                  table.appendRows(tableData);

                  doneCallback();

              });

          };


          tableau.registerConnector(myConnector);


          myConnector.init = function(initCallback) {

              initCallback();

              tableau.submit();

          };


      })();

       

      Thanks Jim Allison