Rare can't parse JSON error

I recently started using ‘JSONEncode’ and ‘JSONDecode’ in my game for the player’s data. Out of thousands of players, I’ve only had a couple report that when opening the game, nothing loads in and they get errors for things being strings when they shouldn’t be, because their JSON failed to decode their data, and then when leaving, the data that wasn’t decoded gets encoded AGAIN if that makes sense. This means that even if the ‘JSONDecode’ doesn’t fail the next time they join, the data will need to be decoded a second time (i think). I don’t know what else would cause the issue.

The only way I’ve been able to replicate the errors that those few players are having is by messing around with my ‘JSONDecode’ function and getting the cant’ parse JSON error, then rejoining.

Here’s where the data is encoded every time the player leaves:

local function encodeData(data) -- I use profile service, and the 'data' here is just the player's profile.Data
	local totalLength = 0
	for i, value in pairs(data) do
		data[i] = httpService:JSONEncode(value)
	end
	return data
end

I tried just encoding the player’s profile.Data but that causes errors with ProfileService so instead I had to loop through each data point within profile.Data.

And here’s where it’s decoded right after their profile loads:

		for i, dataPoint in pairs(profile.Data) do
			if dataPoint == nil then continue end
			if type(dataPoint) ~= "string" then continue end
			local decoded = httpService:JSONDecode(dataPoint)
			profile.Data[i] = decoded
		end

I’m pretty inexperienced when it comes to JSON so I’m not sure why this would ever through up the can’t parse JSON error for some players.

I’d also like to know if there’s anything I can do to fix player’s data that got encoded twice.

Doesn’t datastoreservice (what profilestore has to use internally) automatically convert everything into json? You don’t need to encode/decode anything.

Just store the data as a lua table

I’ve shown you a script that will check if it is already encoded. (in a different post)
I would assume this has been going on for a while, and many players have data that is suffering from this. You may need to design a catch-all that can straighten out the data if this has happened.

Here is a blind guess. It’s not pretty.
I’m tying to apply a counter for decoding…

local HttpService = game:GetService("HttpService")

local function fixCorruptData(data)
	for key, value in pairs(data) do
		if type(value) == "string" then
			local success, decoded = pcall(HttpService.JSONDecode, HttpService, value)
			if success then
				while type(decoded) == "string" do --hope this handles multiple encodings
					local success2, decoded2 = pcall(HttpService.JSONDecode, HttpService, decoded)
					if not success2 then break end
					decoded = decoded2
				end	data[key] = decoded
			else
				warn("Corrupt data at key:", key, "Value:", value)
			end
		end
	end
	return data
end

--usage: profile.Data = fixCorruptData(profile.Data)
1 Like

Thanks for this, it seems to be working in studio. I’ll come back and mark it as a solution if it sorts out the issue when I publish the next update.

1 Like

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