The <GetJSON/> Element

This element permits to obtain data from an app server or JSON file stored in your domain storage. The data then can be used in subsequent commands.

This can be used for example to separate presentation (audio interaction defined in XML) from business rules (back-end processing)

Element Attributes

Attribute Name Description Allowed Values Default Value
var name of the variable where data retrived will be stored non-empty string none
url an http/https/file url a string none
method HTTP method to use for the request "POST" or "GET" "POST"

If the JSON data cannot be obtained (failure to contact the app server, file doesn't exist etc), the var will be set to null (which indicates error)

Example 1: Getting data from server and speak info from it

1
2
3
4
<IVR>
  <GetJSON var="data" url="https://somewhere.com/weather_forecast"/>
  <Speak voice="en-US-Standard-C">Here is today's weather report: {{data.info}}</Speak>
</IVR>

Example 2: Getting a static json file from a remote server (using method GET)

1
2
3
4
<IVR>
  <GetJSON method="GET" var="data" url="https://somewhere.com/weather_forecast/today.json"/>
  <Speak voice="en-US-Standard-C">Here is today's weather report: {{data.info}}</Speak>
</IVR>

Example 3: Getting data from server and taking decisions based on it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<IVR>
  <GetJSON var="data" url="https://somewhere.com/get_account?phone_number={{CallingNumber}}"/>
  <Cond>
    <Clause expr="data.balance > 10000">
      <Transfer>sales1</Transfer>
    </Clause>
    <Clause expr="data.balance > 5000">
      <Transfer>sales2</Transfer>
    </Clause>
    <Else>
      <Transfer>sales3</Transfer>
    </Else>
  </Cond>
</IVR>

Example 4: Getting JSON from file at your domain storage

1
2
3
4
5
6
7
8
9
<IVR>
  <GetDigits validDigits="12" numDigits="1">
    <Speak voice="ja-JP-Wavenet-B">日本語は1 を押してください</Speak>
    <Speak voice="en-US-Standard-C">Press 2 for English</Speak>
  </GetDigits>
  <GetJSON var="data">file://ivr/main/{{Digits}}.json</GetJSON>
  <Speak voice="{{data.voice}}">{{data.greeting}}</Speak>
  <Transfer>{{data.group}}</Transfer>
</IVR>

In the above, the pressed digit will be stored at session parameter 'Digits' that can be used to identify the JSON file to retrieve.

Example 5: Checking if GetJSON was successful

1
2
3
4
5
6
7
8
9
10
11
<IVR>
  <GetJSON var="data" url="https://somewhere.com/weather_forecast"/>
  <If expr="data">
    <Then>
      <Speak voice="en-US-Standard-C">Here is today's weather report: {{data.info}}</Speak>
    </Then>
    <Else>
      <Speak voice="en-US-Standard-C">We could not get the report right now. Please try again later.</Speak>
    </Else>
  </If>
</IVR>