Get Data from API

Hello my friends,

I am using the API skill to get basic data from http://worldtimeapi.org/api/timezone/America/Denver.

I am storing the data in session.temp. However I cannot get the data extracted. Its obviously in an array because if I just attempt to out put the data i get “object object”. I thought it was in a json format so I tried json.parse, but that didn’t work and i got an error saying i was attempting to json.parse a Javascript object.

I then tried accessing the data, as I normally would, from an array by doing session.response.utc_datetime. That failed I tried session.response[2], that failed.

I tried the Axios solution the error came up saying the bot didn’t have axios (which is contradicting what the forum and documentation show)

Thoughts? feelings? what am I missing?

@Sara_Reece check out a similar question asked by me earlier and how Botpress team helped me reach a solution.

Also, visit my blog for Botpress related tutorials.

@abhisheksimion the issue is I am not using a custom component I am using their built in API which is supposedly saving the input gleaned from my get command into a regular variable. Here is a video going over what I mean .
I have actually book marked your blog it has been very helpful on many matters.

@Sara_Reece

I usually use JSON.stringify(session.response), which would give you the details. Copy that to an editor (e.g. Notepad++) and beautify the JSON if it is long, you should be able to find your way.

Thank you! that did the trick! I believe I can run with it from here

Okay here is the solution:

I used JSON.stringify to see what the object was actually saving. By doing so I realized i needed to ask for the data by doing this

session.variableName.body.variableExtracting

Because of how Javascript saves arrays my data was being stored in the variable body and then in the variable name rather than just in variable name.

So I couldn’t just say session.variableName.variableExtracting. I needed to add body in as shown above.