HttpService JSONEncode data truncated

What’s wrong with this code?

local data = httpService:JSONEncode({
	gameId = game.PlaceId,
	game = game.Name,
	userId = player.UserId,
	username = player.Name,
	nickname = player.DisplayName,
	isPremium = player.MembershipType == Enum.MembershipType.Premium,
	platform = 0,
	joinedDate = DateTime.now():FormatLocalTime("lll", "it-it")
})

If I print data, I will get a truncated string or sometimes an empty string.
Problems are isPremium and joinedDate.

2 Likes

Wrap them in [“”]s

["gameId"] = game.PlaceId
2 Likes

I already tried that but the problem continues to be there.

1 Like

Ah I see. For the isPremium, wrap the statement in ()s. The joinedDate is a Roblox datatype and not something JSON encoded tables can store. Convert it to a string.

FormatLocalTime returns a string so I don’t need to convert it to a string.
Anyway I tried but still not work. I’m pretty sure that it’s a bug.

I tried to make a custom function to Encode a JSON however, I don’t know for what strange reason, the problem continues to occur:

local function JSONEncode(tableToEncode)
	local json = "{"
	for k, v in pairs(tableToEncode) do
		if typeof(v) == "string" then
			v = "\""..v.."\""
		else
			v = tostring(v)
		end
		json = json.."\""..k.."\":"..v..","
	end
	return json:sub(0, -2).."}"
end

Seems to be working perfectly with me. Are you sure it isn’t another part of your code?

image

local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")

Players.PlayerAdded:Connect(function(player)
	local data = HttpService:JSONEncode({
		gameId = game.PlaceId,
		game = game.Name,
		userId = player.UserId,
		username = player.Name,
		nickname = player.DisplayName,
		isPremium = player.MembershipType == Enum.MembershipType.Premium,
		platform = 0,
		joinedDate = DateTime.now():FormatLocalTime("lll", "it-it")
	})
	
	print(data)
end)

As @Swetch29 mentioned, your data code is working perfectly for me as well. Perhaps the issue is another part of your code. Have you tried printing the player instance before the local data line, or printing the player.MembershipType or DateTime.now():FormatLocalTime("lll", "it-it") statements?

Also, did you print the data directly using the built-in print function after the JSON encoding to check if this is the cause of the issue? If it still occurs then perhaps a studio issue, maybe caused by a setting?

1 Like