JSON data from external API returns error

Hello fellow bro developers!

Now I want to get data of Dow Jones Index for the last 30 days into Roblox Studio.
I use the external API and wrote the code as follows:

Blockquote
local HttpService = game:GetService(“HttpService”)

local ticker = ‘DJI’

local interval = ‘1day’

local outputsize = 30

local api_url = “https://api.twelvedata.com/time_series?symbol={"..ticker.."}&interval={"..interval.."}&outputsize={"..outputsize.."}&apikey={api_key}

local response = HttpService:GetAsync(api_url)

local data = HttpService:JSONDecode(response)

local mainBoard = script.Parent

local textLabel = mainBoard.SurfaceGui.Frame.TextLabel

local dji = game.Workspace.DJI

local part = dji.Part

local dateLabel = dji.Part.SurfaceGui.Frame.DateLabel

dateLabel.Text = os.date("%Y-%m-%d", os.time(os.date(’*t’)) - 86400)

print(data.values[1].datetime)

Blockquote

As you can see, “attempt to index nil with number” is displayed,
but I don’t know what is wrong.
I will appreciate if someone help me.

Insert ur code inside ``` to format it.

local HttpService = game:GetService(“HttpService”)

local ticker = ‘DJI’

local interval = ‘1day’

local outputsize = 30

local api_url = “https://api.twelvedata.com/time_series?symbol={"..ticker.."}&interval={"..interval.."}&outputsize={"..outputsize.."}&apikey={api_key}”

local response = HttpService:GetAsync(api_url)

local data = HttpService:JSONDecode(response)

local mainBoard = script.Parent

local textLabel = mainBoard.SurfaceGui.Frame.TextLabel

local dji = game.Workspace.DJI

local part = dji.Part

local dateLabel = dji.Part.SurfaceGui.Frame.DateLabel

dateLabel.Text = os.date("%Y-%m-%d", os.time(os.date(’*t’)) - 86400)

print(data.values[1].datetime)
1 Like

Thank you for your reply

Blockquote
local HttpService = game:GetService(“HttpService”)
local ticker = ‘DJI’
local interval = ‘1day’
local outputsize = 30

local api_url = “https://api.twelvedata.com/time_series?symbol={"..ticker.."}&interval={"..interval.."}&outputsize={"..outputsize.."}&apikey={api_key}

local response = HttpService:GetAsync(api_url)

local data = HttpService:JSONDecode(response)

print(data.values[1].datetime)

Blockquote

Btw cud u print data and show us the output.

@HubMong

This is the screen when I used a python editor, where you can see the data is imported properly.

I noticed that in the screen shot a param in the url “api_key” seems to not have an api key, is that perhaps the problem? Or did you remove it (which makes sense)

1 Like

This is the error message in Studio when I use print method.

Just try to print data. blah blahchar limit

1 Like

Yes, thank you for your advice. I changed the code as follows, but got the same error.

Try printing just data then, maybe we can see the problem

1 Like

Thank you for your smart advice. I tried and the result is as follows:

Now try printing data[1] like you did before

1 Like

Try this:

print(data.values[0].datetime)

Update me with the result

1 Like

nil was returned.

could you try

print(response)

to see the structure of the response

1 Like

Same error message

I got the message as follows:

well, that means that there is an error with your request, not roblox…

local url = string.format("https://api.twelvedata.com/time_series?symbol=%s&interval=%s&outputsize=%d&apikey=%s", ticker, interval, outputsize, api_key)

Ur url was completely wrong. Replace ur url with the above one. I had to look up the api, so pretty sure the above one works.

Pls update me if the above works @HubMong.

1 Like

Now I confirmed it work perfectly.
Thank you for your kind supports, guys!

2 Likes