Can't parse JSON

Script:

local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync({
	Url = "http://games.roproxy.com/v1/games/votes?universeIds="..4623776635,
	Method = "GET"
})

local votes = HttpService:JSONDecode(response.Body)
print(votes)
print("upVotes:", votes.data[1].upVotes)

I’m trying to make it print the amount of likes, but it says Can’t parse JSON

Are you sure the body is valid JSON?

That error appers whenever what you pass to JSONDecode() isn’t a valid json string.

You should always wrap API calls in pcalls to handle errors

local success, result = pcall(function()
    return HttpService:RequestAsync(...)
end)

if not success or not result.Success then
    return warn(`Unsuccessful request, failed with status code {result.StatusCode}.`)
end

if result.Body then
    print(HttpService:JSONDecode(result.Body))
end

Apparently you are receiving an error instead of the expected response which doesn’t include a valid json body. You can debug it by reading the response’s status code.

1 Like

It says: Unsuccessful request, failed with status code nil.

That means that the error is not in the request because the return of RequestAsync() is a table with a StatusCode key. You probably haven’t turned on HttpRequests in your game’s settings. Try printing result.

1 Like