Can't parse JSON in game, but works in studio

I’m having issues with me trying to get the server location. I followed this tutorial specifically: How to get server location. No API Key Required

for some reason it’s erroring at:

if ServerData then
		ServerData = HttpService:JSONDecode(ServerData)
	end
local suc, getIp = pcall(function()
		return HttpService:GetAsync("https://ipconfig.io/json")
	end)

	local serverIP

	if getIp then
		local ipData = HttpService:JSONDecode(getIp)
		serverIP = ipData["ip"]
	end

	local Success, ServerData = pcall(function()
		return HttpService:GetAsync("https://ipapi.co/"..serverIP.."/json/") 
	end)

	if ServerData then
		ServerData = HttpService:JSONDecode(ServerData)
	end

	for _, sufgui in pairs(CollectionService:GetTagged("info")) do
		local frame = sufgui:FindFirstChild("Frame")

		if not frame then return end

		for _, textlabel in pairs(frame:GetChildren()) do
			if not textlabel:IsA("TextLabel") then
				continue
			end

			if not ServerData[textlabel.Name] then
				continue
			end

			textlabel.Text = string.upper(textlabel.Name..": "..ServerData[textlabel.Name])
		end
	end

I’m not good with JSON can anyone help me figure out what I need to do?

What error is it returning? Try printing the getIp result and check what it actually returns. If everything looks fine, then try publishing new version of the game and retry.

It errors “Can’t parse JSON”.

Here’s what it outputs:

{
                    ["asn"] = "********",
                    ["asn_org"] = "*********",
                    ["city"] = "********",
                    ["country"] = "********",
                    ["country_eu"] = false,
                    ["country_iso"] = "US",
                    ["ip"] = "********",
                    ["ip_decimal"] = ********,
                    ["latitude"] = ********,
                    ["longitude"] = ********,
                    ["metro_code"] = ********,
                    ["region_code"] = "********",
                    ["region_name"] = "********",
                    ["time_zone"] = "********",
                    ["user_agent"] =  ▶ {...},
                    ["zip_code"] = "********"
                 }

I added the asterik’s (********), also i’ve been publishing hoping it was a roblox issue.

This looks like it’s already decoded lua table, maybe you are decoding json result twice?
(oh yeah this is the getip result so it should be decoded)

You should be confirming that Success is even true before trying to do anything with ServerData. It’s most likely that the parsing is failing because Success is false and ServerData is in turn the error message HttpService returned.

1 Like

This solves the error but I still don’t know why it never prints true in game…

The API you are trying to use likely gets used quite a lot in Roblox games leading to that service provider rate limiting requests from Roblox servers. When you make that same request in studio it is coming from your own device and not a Roblox server so it isn’t being rate limited.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.