VoiceGuide IVR Software Main Page
Jump to content

Getting all values for specific field from returned JSON or XML arrays

Recommended Posts

When the data returned to VoiceGuide contains arrays, it is sometimes required for all the values of specific field to be extracted into one Result Variable (RV).

Here is the approach that can be used (code is in VBScript):

   counter = 1
   separator = "|"
   concatenated = ""
   
   rv_name = "module_name_returned_path_" & counter & "_field_name"
   rv_value = vgo.RvGet($RV_LINEID, rv_name)
   
   Do While rv_value <> ""
     If concatenated = "" Then
       concatenated = rv_value
     Else
       concatenated = concatenated & separator & rv_value
     End If    
     counter = counter + 1
     rv_name = "module_name_returned_path_" & counter & "_field_name"
     rv_value = vgo.RvGet($RV_LINEID, rv_name)
   Loop
   
   vgo.RvSet $RV_LINEID, "AllFieldNameEntries", concatenated
   vgo.Run_ResultReturn $RV_LINEID, "success"

In the above approach all the values from RVs: $RV[module_name_returned_path_X_field_name] will be concatenated into $RV[AllFieldNameEntries]

(separated by character assigned to "separator" variable)

Share this post


Link to post

Here is an example demonstrating operation.

A short demo VoiceGuide script was created which uses a Web Service module to retrieve a 7 day 'hourly' weather forecast from weather.gov provided web service. The following "Run Script" module then extracts the temperatures from returned data and puts them in a comma separated list. TTS is then used to read out the temperature list.

The VoiceGuide Script looks like this:

script.png.e0b19e9d374204309551fe0c5ed1d1e2.png

Here is actual script used:

test_ws_weather_gov.vgs

 

The Web Service module performs a GET to this address: http://api.weather.gov/gridpoints/OKX/32,34/forecast/hourly

(You can click on above link to see the current returned data. The grid-points used correspond to location of New York City)

The returned data from weather.gov is:

weather.gov_returned_data.txt

The above returned data creates $RVs which then allow individual entries from returned JSON to be accessed from other modules in script.

The module "Get Temp List" uses this VBScript to access all the temperatures and put them in a list:

counter = 1
separator = ","
concatenated = ""

rv_name =  "properties_periods_" & counter & "_temperature"
rv_value = vgo.RvGet($RV_LINEID, rv_name)

Do While rv_value <> ""
 If concatenated = "" Then
   concatenated = rv_value
 Else
   concatenated = concatenated & separator & rv_value
 End If    
 counter = counter + 1
 rv_name = "properties_periods_" & counter & "_temperature"
 rv_value = vgo.RvGet($RV_LINEID, rv_name)
Loop

vgo.RvSet $RV_LINEID, "AllFieldNameEntries", concatenated
vgo.Run_ResultReturn $RV_LINEID, "success"

 

NB. This JavaScript works the same in the "Get Temp List" module:

//JavaScript

var counter = 1;
var separator = ",";
var concatenated = "";

rv_name =  "properties_periods_" + counter + "_temperature";
rv_value = vgo.RvGet($RV_LINEID, rv_name);

while (rv_value != "")
{
 if (concatenated == "")
 {
   concatenated = rv_value;
 }
 else
 {
   concatenated = concatenated + separator + rv_value;
 }
 counter = counter + 1;
 rv_name = "properties_periods_" + counter + "_temperature";
 rv_value = vgo.RvGet($RV_LINEID, rv_name);
}

vgo.RvSet($RV_LINEID, "AllFieldNameEntries", concatenated);
vgo.Run_ResultReturn($RV_LINEID, "success");

 

The list of temperatures is stored by above script in $RV[AllFieldNameEntries] and is then read out using TTS in the "Say Temp List" module.

Google Cloud was used as TTS engine in this example, and it completed returning  the 2 minute long .WAV file back in 3 seconds:

tts_219_1.wav

For completeness here is the vgEngine trace capturing the call:

weather.gov_vgengine.txt.zip

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×