ThingWorx Model Definition and Composer > Data Shapes > Infotables
  
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 Things.
What is an Infotable?
An infotable is a zero-indexed, ordered array of objects that expose the same properties. Infotables can be represented in any programming language. On ThingWorx platform, infotables are commonly used in JavaScript implementations and may be used to represent input values and output values for services. The ThingWorx Edge SDKs all support the use of infotables.
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. When using infotables in a custom service or application, 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?
ThingWorx allows you to use JavaScript to create custom services, using infotables for input and output. 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-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.
On line 2 above, the undefined value is not an error. Although the declaration of a local scope variable (var) returns the object undefined as a result, 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 typing 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 you can see 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 declare data shapes in a ThingWorx Edge SDK application and in ThingWorx Composer. Data shapes are required to build a functional infotable. Essentially they are a schema mechanism for defining the required fields of a thing in ThingWorx. For more information about data shapes, see Data Shapes.
How Do I Use an Infotable with a Service?
The ThingWorx Edge Java SDK has tools for constructing infotables that are needed for invoking services. This SDK provides the basis for showing how you might use an infotable with a service. Note that an INFOTABLE is one of the ThingWorx base types. In particular, it is an aggregate base type.
Services have arguments and return values that are expressed using primitives such as String, Integer, Number, and Boolean. An infotable can be used to describe a set of service arguments. Service parameters and return values can be passed and received as infotables that contain one or more rows.
When used in a ThingWorx Edge Java SDK application, an infotable has a DataShapeDefinition that describes the names, base types, and additional information about each field within the table. Data within an infotable is contained in rows. Each row can have one or more fields, described by the DataShapeDefinition of the infotable. Here is an example of an infotable definition, with its data shape and rows:
In addition to using infotables with services, you can use infotables to hold data for an event or the values that are returned by a read request for multiple properties. The infotable can contain one or more rows. When results are returned, you need to extract the values from each row, using the Java SDK methods, getFirstRow(), getSecondRow(), and so on. You also use a helper function that matches the base type for the field, such as getStringValue().
Here is an example of using an infotable to return the results of a property read:
// Define result as an InfoTable
InfoTable result

// Reading the "name" property of a Thing and
// extracting the results from an InfoTable
// Since "name" is a STRING, use getStringValue()
// to return the value of the "name" property.
result = client.readProperty(ThingworxEntityTypes.Things,
ThingName, "name", 10000);
String name = result.getFirstRow().getStringValue("name");
For more information about data shapes and infotables with the ThingWorx Edge Java SDK, see http://support.ptc.com/help/thingworx_hc/thingworx_edge_sdks_ems/#page/thingworx_edge_sdks_ems%2Fjava_sdk%2Fc_javasdk_data_shapes.html.