JSON decoding, instead of 2 index on table. Shown nothing

Json Code:

{
"TotalDonation" : 0,
"RecentDonator" : []
}

Hello Developers!
im confused on what’s wrong with my script. In here, I’m decoding the string json but instead of this


it did it like this
image
. the TotalDonation and the RecentDonator doesn’t exist or nil.

Check the count of table:

image
Output: 0 (Expected = 2)
image

Script Error:

image
Line script:
image


Value from:

image


Table from


JSON Format

image


raw
local function updateRecent()
	if _GData:GetAsync(game.GameId..'--key') then
		print(tostring(_GData:GetAsync(game.GameId..'--key')))
		local LUAU_Data = HttpService:JSONDecode(tostring(_GData:GetAsync(game.GameId..'--key')))
		warn(#LUAU_Data)
		print(LUAU_Data)
		for _, players in pairs(game.Players:GetPlayers()) do
			for index, value in pairs(LUAU_Data) do
				local __D = game:GetService('ReplicatedStorage').DonatorTemplate:Clone()
				__D.Parent = players:WaitForChild('PlayerGui'):WaitForChild('ScreenGui').Frame.Leaderstats.RecentDonator
				__D.Raised.Text = value
				__D.Username.Text = index
			end
		end
	end
end

if my explanation is not quite accurate, you can ask me so you could further look for more details. (i feel like that my explanation is bad that’s why i put this here)

The JSON should start and end with brackets not curly brackets

1 Like

image
well, do you know how to fix this?

Bracket string syntax is only two brackets, though in this case it looks like you’re trying to scope the string because you’re already using brackets.

Instead of adding a third bracket, you want to scope it by adding an equal sign.

local JSON = [=[
text goes here
]=]

When using this syntax, you terminate the string you just need to make sure you have the same number of equal signs as the start and end:

local JSON = [[
this also works with 0 equals
]]

{
"TotalDonation" : 0,
"RecentDonator" : []
}

this json returns 0 because it decodes as a dictionary. In Lua, the # operator will only be able to count array values.

To get a count of a dictionary, you can iterate over the keys like so:

local function count(dictionary)
    local sum = 0
    for key in pairs(dictionary) do
        sum += 1
    end
    return sum
end
1 Like

it works, but do you know how to fix this?
image

it seems like there’s values in the index
image
image

But it gave me nil at here
image

nvm. i instead use or replace 2 with the index name

1 Like