fair-rose
fair-rose17mo ago

How to handle an API Response that is an Array?

I get this response from an API Call { "query": "Wie macht man einen Kuchenbasar bitte?", "follow_up_questions": null, "answer": null, "images": null, "results": [], "response_time": 1.14, "VF_STATUS_CODE": 200, "VF_HEADERS": { "connection": "close", "content-length": "139", "content-type": "application/json", "date": "Wed, 24 Apr 2024 08:40:06 GMT", "server": "nginx" } } In "results" is an array of URLs, i want to use in the further flow. how do i handle the array?
4 Replies
Tico | Voiceflow Assistant
To handle an API response that is an array in Voiceflow, you can use the "Capture Response" section in the API block. This allows you to save the information you received from the API call to a variable in Voiceflow. In your case, if you want to save the "results" array from your API response, you can do this by copying the object path or key and pasting it into the text box in the "Capture Response" section. Here's how you can do it: 1. In the "Capture Response" section, enter the path to the array in the API response. In your case, the path would be "response.results" as "results" is the key of the array in the API response. 2. Select a variable that you would like to save the array to. You can create a new variable for this purpose. Let's say you create a new variable named "resultsArray". 3. Now, the "results" array from the API response will be saved to the "resultsArray" variable in Voiceflow. Once you have saved the array to a variable, you can use it in your flow. For example, you can use a "Speak" block to speak back the URLs in the array to the user. You can access the individual elements of the array using their index. For example, "resultsArray[0]" will give you the first URL in the array, "resultsArray[1]" will give you the second URL, and so on. Remember, arrays in programming are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on. Please note that if the "results" array is empty, trying to access an element in the array will result in an error. So, you might want to add a condition to check if the array is not empty before trying to access its elements.
fair-rose
fair-roseOP17mo ago
"resultsArray[0]" , {resultsArray{0}}, {resultsArray}[0] <- this is not working. how do i access the array alements?
Julien
Julien17mo ago
I would ask ChatGPT directly.
deep-jade
deep-jade17mo ago
response.resultArray[0].title if your getting the title of the first element of the array for example

Did you find this page helpful?