Can't parse JSON

I’ve made a script to detect if a player is in a group and if yes what is id, but somehow, I got this error:
image

Here the script:

local Http = game:GetService("HttpService")

game:GetService('ReplicatedStorage').Events.GetPlayerInfo.OnServerEvent:Connect(function(player, UserId)
	local Table = Http:JSONDecode('https://groups.roblox.com/v2/users/' .. tostring(UserId) .. '/groups/roles')
	
	for _, v in pairs(Table) do
		if Table.group.id == 8194141 then
			if Table.group.rank == 4 or Table.group.id == 255 then
				return true
				
			else
				return false
				
			end
		end
	end
end)

2 Likes

you’re not using JSONDecode() properly, you need to send a GET request using Http:GetAsync('https://groups.roproxy.com/v2/users/' ..tostring(UserId).. '/groups/roles') then put up Http:JSONDecode(result)

2 Likes

If I do what you said to me, I don’t have access to roblox ressources
image

the script:

local Http = game:GetService("HttpService")

game:GetService('ReplicatedStorage').Events.GetPlayerInfo.OnServerEvent:Connect(function(player, UserId)
	local result = Http:GetAsync('https://groups.roblox.com/v2/users/' .. tostring(UserId) .. '/groups/roles')
	local Table = Http:JSONDecode(result)
	
	for _, v in pairs(Table) do
		if Table.group.id == 8194141 then
			if Table.group.rank == 4 or Table.group.id == 255 then
				return true
				
			else
				return false
				
			end
		end
	end
end)

replace https://groups.roblox.com/v2/users/ with https://groups.roproxy.com/v2/users/

1 Like

Now it’s say that I have sent a bad request,
image

local Http = game:GetService("HttpService")

game:GetService('ReplicatedStorage').Events.GetPlayerInfo.OnServerEvent:Connect(function(player, UserId)
	local result = Http:GetAsync('https://groups.roproxy.com/v2/users/' .. tostring(UserId) .. '/groups/roles')
	local Table = Http:JSONDecode(result)
	
	for _, v in pairs(Table) do
		if Table.group.id == 8194141 then
			if Table.group.rank == 4 or Table.group.id == 255 then
				return true
				
			else
				return false
				
			end
		end
	end
end)

Sorry I don’t use that much HttpService

2 Issues, the main issue is that you aren’t making an HTTP request, so instead of the JSONDecode getting a valid json(the response of the request) it gets the url itself as the argument to decode, causing the error. The other issue is that Roblox servers can’t send requests to Roblox domains, so you must use a proxy. For serious APIs that use authentication you should not use public proxies, but since this is openly available information from a non-authentication API the risk is smaller, so you can use a proxy like roproxy.com. So your code should be:

local proxy = "roproxy.com"
local URL = "https://groups.roblox.com/v2/users/"..tostring(UserId).."/groups/roles"
local response = Http:GetAsync(URL:gsub("roblox.com", proxy))
local Table = Http:JSONDecode(response)
print(Table)
--do other things

Keep in mind there’re still risks with public proxies. For example, the proxy owner could go mad and send you false information or keep information about the IP/Roblox machine sending the request so it can DDOS it or do something malicious.

1 Like

Using this, I have the same error: Bad Request

Do print(UserId), perhaps the user id is invalid(for example it may be a dummy user, that has a fake id like -1, -2, -3 etc.).

Also you can use player.UserId directly if the user id is the user id of the player firing the remote.

is because you’re attempting to use the JSONDecode method of the HttpService to directly parse a URL, which is not supported. The JSONDecode method is used to decode JSON string

ou need to use the HttpGetAsync method of the HttpService to fetch the JSON data from the URL, and then parse it using JSONDecode

local Http = game:GetService("HttpService")

game:GetService('ReplicatedStorage').Events.GetPlayerInfo.OnServerEvent:Connect(function(player, UserId)
	local response = Http:GetAsync('https://groups.roblox.com/v2/users/' .. tostring(UserId) .. '/groups/roles')
	local success, decoded = pcall(Http.JSONDecode, Http, response)
	
	if success and decoded then
		for _, group in ipairs(decoded.data) do
			if group.group.id == 8194141 then
				if group.group.rank == 4 or group.group.id == 255 then
					return true
				else
					return false
				end
			end
		end
	end
end)