The JSON Array iterator build tool is used to create a section of your knowledge base which takes a stringified JSON Array of Objects and performs whatever operations are required on each object of that array. A typical use case for this requirement is extracting a number of records from a database and performing a set of operations on the fields of each returned record.
There are 2 main elements to the viabl.ai iterator pattern...
A typical section of knowledge produced by the build tool (if the tool is dragged onto a decision tree) looks like this
The initial page prompts for a name for the iterator object, the catalog category for this iterator object and the Text Question which holds the stringified JSON array. If the JSON input Text Question does not yet exist, it can be created in the build tool.
The build tool allows for the supply of a sample JSON Input. This can either be an array of objects or a single example object. Again, a typical use case might be a document (i.e. row) returned from a MongoDB database. This sample object is used to pre-populate the next tool page. So a simple object representing some data held in a customer database might look like...
{
"name": "bob",
"age": 25
}
The Mapping page handles the mapping of object properties to existing viabl.ai questions. Not all properties need to be mapped and any missing properties (or if the sample JSON was left blank) can be added here.
The build tool creates JavaScript code from your mapping definition and associates it with the new iterator object. If you edit the iterator object, you'll see the code generated to perform the mapping (on a single record at a time) from the JSON object to the catalog Questions. If more properties are added in future or removed, this code can be edited accordingly.
The iterator object is designed to be placed on a Decision Tree (see above image) with looping, processing of the record, error handling and completion steps.
The current record number (1 based) can be accessed via the currentidx property of the iterator object, for example...
console.log(myIteratorObj.props.currentidx);
If the JSON held in the tied jsontextobj is not valid, the iterator will return Error and the specific error message can be accessed via the parseerror property, for example...
xpertrule.messasge("JSON error: " + myIteratorObj.props.parseerror);