HttpService:JSONEncode ignores some keys of a table

When using HttpService:JSONEncode on a table with empty spaces between indexes, the values for those indexes are not included in the result. It also only includes values for integer indexes in tables that have both integers and strings as keys. Nevermind, this one is mentioned in the documentation.

Since I don’t know how to explain it very well, here is an example:

local httpService = game:GetService("HttpService")
local t = {
	[1] = "hi",
	-- no value for 2
	[3] = "bye"
}

print(httpService:JSONEncode(t))

Output: ["hi"]

It would be expected for the output to include the values for both 1 and 3, not just 1. It seems to stop if it doesn’t find another number after 1, but it would make more sense for it to include all keys.

If this bus can’t be fixed due to backward-compatibility reasons, possibly 2 more functions of HttpService could be added (for :JSONEncode and :JSONDecode) with fixed versions of these functions. It’s very hard to work with encoding tables when some keys in tables are ignored by the :JSONEncode function.

I know that this bug report is hard to understand, but I don’t exactly know the right terminology for these things. Hopefully the examples are enough for you to understand what I am trying to say.

What is your expected behavior? ["hi",null,"bye"]? What if the table has one entry at 1 and one entry at 100000?

JSON requires dictionary keys to be strings, too, so you can’t just turn it into a dictionary at serialization.

8 Likes

:grimacing: After rereading what I wrote last night, it sounds really dumb. When I wrote it I was tired and unhappy that my saving system didn’t work because of this “bug”. Sorry!

I think I’ll just make a custom table to string converter instead of using JSONEncode.

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