Skip to main content
Kinetic Community

Looping Through One Column of Customer Answer Table

Once you have collected answers in a table format and saved them into a JSON as specified in this solution, you will then want to use them. This solution describes how to loop through a column of the stored data to allow for some possibilities on how to process/use these answers.

Usage

What you do with the table stored in the JSON string will entirely depend on what you want to do with it. 

You may want to loop through the rows in the table and perform actions based on each a data point in a column. To do this, you will need to convert the JSON into XML with this handler. Details of constructing a loop in this data are included in the handler page. This same thing can be accomplished, with a little more precision by using the JSON Parse handler to first parse out the desired column, then loop through those results. This gives you the ability to filter out unique values if desired. 

Example

The pictured example is included in the updated Table of Answers service item attached to this solution record. Note that you must load the related/refrenced handlers into your systems before the examples will function. Example trees are inactive. Activate them one at time, activating Loop Through One Column to test this example.

The loop through column example contains three paths. The one on the left uses the Utility JSON to XML handler and uses those results as the datasource of the loop. The example in the middle uses a different technique, using the Utility JSON Parse to pull the desired data out of the json, in this case unique values, and create a new XML of that column, which is used as datasource for the loop. The example on the right uses the Utility JSON Parse to pull the desired data out of the json, in this case all values, and create a new XML of that column, which is used as datasource for the loop. In the third example, the column is integer values that the example finds the maximum of and the sum of.

LoopThroughOneColumn-all.png

Loop Through One Column - Example 1

LoopThroughOneColumn-toXML.png

The loop through column example contains two trees. The one on the left uses the Utility JSON to XML handler and uses those results as the datasource of the loop. The loop path for these answer tables are

//node[@type="Array"]/node[@type="Object]/node[@name="columnName"]

This example loop just echos the column values to allow you to see the results in the Task Management Console. In practice, you could do anything with the values within the loop.

Loop Through One Column - Example 2

LoopThroughOneColumn-ParseUnique.png

The example on in the middle uses a different technique, using the Utility JSON Parse to pull the desired data out of the json, in this case unique values, and create a new XML of that column, which is used as datasource for the loop. The loop path for this is as specified for the Utility JSON Parse handler.

//items/item

Loop Through One Column - Example 3

LoopThroughOneColumn-ParseAll.png

The example on the right also uses the Utility JSON Parse to pull the desired data out of the json, in this case all values, and create a new XML of that column, which is used as datasource for the loop. The loop path for this is as specified for the Utility JSON Parse handler.

//items/item

The values echoed in this case are processed in two different ways using the techniques listed here. One node finds the maximum of the column.

<%=sum=0
@results['Echo All Quantity'].collect{|key,values| sum = sum + Integer(values['output']) }.inspect
sum%>

Another node finds the sum of all the values of the column.

<%=array = []
   @results['Echo All Quantity'].collect{|key,values| array.push(values['output']) }.inspect
array.max %>