Getting to Know Infotables
The infotable is an often used and sometimes misunderstood data structure in the ThingWorx Platform. Since many service calls use infotables as input parameters and as output return values it is hard to avoid them Once you understand them, you may even want to use them in your own client applications.
What is an Infotable?
An infotable can be represented in any programming language as a zero-indexed, ordered array of objects that expose the same properties. They provide a simple way to communicate their structure to users. The ThingWorx Edge MicroServer supports the use of infotables. On ThingWorx Platform, infotables are commonly used in JavaScript implementations.
Since an infotable is an array of objects, it can also be thought of as a spreadsheet where the array entries are rows and the object properties of each object in the array are the columns. Since we are trying to represent tabular data, it is important that each object have the same properties so that each column of the table can map to a property value.
How Do Arrays Work in JavaScript?
If you have ever worked with an Array object in JavaScript, you are familiar with how arrays work in ThingWorx. For those who may want a refresher or have not worked with an Array object in JavaScript, let's go over the basics.
To declare an array, you can use the following:
var myArray=new Array();
var mySecondArray=[];
Both of the above statements are equivalent. They both create array objects. An array can contain multiple objects in sequence of any type. For example:
var myArrayOfStuff=[“Pickel”,3,{name:”Bill”,phone:”555-1212”}];
The above array contains a String, a number and a custom object with the fields name and phone. If you want to try this out for yourself, use the Chrome browser and open the JavaScript Console (CTRL+SHIFT+J). Then paste in the line above to create this array, and try the following examples to access the items in the array. Below is screen shot of the JavaScript console in Chrome showing how to create and the access each of the objects in the JavaScript array.
The "undefined" value is not an error on line 2 above. The declaration of a local scope variable (var) returned the object undefined as a result, but it does create the variable.
How is an Infotable Different from an Array?
In the example above, the JavaScript array contains completely different types of objects. infotables can contain only one type of object. This type is defined, not with JavaScript's native object type-ing mechanism, but through the use of another ThingWorx-specific typing definition, called a Data Shape. JavaScript considers every type of object that is stored in an infotable to be of type “Object” as shown above when the command myArrayOfStuff[2] is printed out. Infotables require that each object in the array expose the exact same properties.
What is a Data Shape?
A Data Shape is a specification of the required property names and return types that each property of an "object" must have to be added to an infotable. Data shapes also contain other metadata about a property that is useful for formatting and displaying the infotable in a tabular format or Grid. You can create new and add or edit existing Data Shapes in ThingWorx Composer. Further, you can apply Data Shapes to Things that represent your Edge devices to allow them to take advantage of ThingWorx Asset Advisor features such as Software Content Management (SCM). Data shapes provide a schema mechanism for defining the required fields of a Thing in ThingWorx.