1

I am using Twilio IVR studio. I am using http request widget and know I can call 'widgets.MY_WIDGET_NAME.parsed.[parsed variable name]' to get the returned json data.

Say like I have the above widget return 7 jobs inside the array of hashes. I just want to return the job_id for each one and not the other data inside it. How would I got about doing that with liquid? I know there is loop but having a hard time getting.

I can do something like widgets.MY_WIDGET_NAME.parsed.[parsed variable name][0].job_id and it returns the first id. Any help would be appreciated.

   "parsed": {
    "success": "7 visits found",
    "visits": [
      {
        "job_id": "12344",
        "check_in": "",
        "check_out": ""
      },
      {
        "job_id": "12344",
        "check_in": "",
        "check_out": ""
      },
      {
        "job_id": "12344",
        "check_in": "",
        "check_out": ""
      },
      {
Awhitey98
  • 167
  • 1
  • 2
  • 14

2 Answers2

2

Twilio developer evangelist here.

Twilio Studio supports Liquid templates. You can iterate a number of ways in Liquid including using a for loop. So you could do something like:

{% for job in widgets.MY_WIDGET_NAME.parsed.[parsed variable name] %}
  {{ job.job_id }}
{% endfor %}

Let me know if that helps.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks philnash! I worked on something similar to what you had. There is a ton you can do with these widgets. – Awhitey98 Oct 04 '18 at 11:35
  • I am continually surprised with the power there too, and I work here! – philnash Oct 04 '18 at 11:36
  • If I have an Http Request widget what would you typically put inside the the "Request Body" section? I know and have set up Params for mine but not quite sure what would go there – Awhitey98 Oct 04 '18 at 11:39
  • Building this IVR with studio saved me a lot of headache with having creating multiple apps. – Awhitey98 Oct 04 '18 at 11:41
  • If you use an http request widget for a GET request, then you don't need anything in the body. If you're making a POST request, that's where I'd put the data that you want to send as part of that request, you can do so as a form encoded request or JSON. The data you actually add all depends on the request you are making. – philnash Oct 04 '18 at 14:30
  • Thanks! All of my requests are posts hitting my internal platform api calls. Right now all I have inside the http request are http params I have set up. Do you by chance have an example of the what I would send inside the Http request body? – Awhitey98 Oct 04 '18 at 17:11
  • What you send in the body depends on where you are sending the HTTP request. I can't answer that, especially if it's an internal API. – philnash Oct 05 '18 at 06:28
0

this note might help: Parse JSON into array javascript with Twilio Function

if not, this might: https://www.twilio.com/docs/studio/widget-library/run-function (under the Response heading, it's the description for Content Type)

details: i asked the same question of the NickH at Twilio and got this gem back that i have permission to share: "(Twilio) automatically parses JSON payloads returned to Function widgets and HTTP Request widgets! However, it has to be a JSON object, not an array. (don't worry, it can still contain an array). Instead of this:

[
  {},
  {},
  {}
]
try returning this:


{
  "result":[
    {},
    {},
    {}
  ]
}

You'll access these variables as {{widgets.NAME_OF_FUNCTION_WIDGET.parsed.result[index]}}​(index being an integer starting from 0, for this example, 0, 1, and 2)."

hth!