JSON Encode Questions | Creating "Save Slots"

Hello! I’m attempting to create a “Save Slot” UI where a lot of data characteristics of a model needs to be saved. I want to do this using the JSON Encode and Decode method but I only know how to save 1 specific table each time, I’m not sure how I’d go on about getting previous “data” from the JSon value and then add the new json data to that.

A better way to understand this is I’m trying to enabled to save multiple “save slots” but all in one saved “String Value” in the DataStore.

local CurrentDataList = {}
				
				
				-- player:FindFirstChild("Data_Folder").BoothData
				local DataStat = game.ReplicatedStorage.Testing
				
				
				if DataStat.Value ~= '' then -- getting previous saved data information (doesn't work)
					      local LoadedData = HttpService:JSONDecode(DataStat.Value)
			
						table.insert(CurrentDataList,LoadedData)
						print(LoadedData)
				
                end
				
				
				
				
				
				local nameFile = filename
				local BoothModel = x:FindFirstChild("StandType")
				local BaseColor = x.Base:FindFirstChild("FullColor")
				local CounterColor = x.Counter:FindFirstChild("FullColor")
				local Message = x.Display.DisplayPart.SurfaceGui:FindFirstChild("Message")
				local ImageID = x.Display.DisplayPart.SurfaceGui:FindFirstChild("ImageID")
				local Layout = x.Display.DisplayPart.SurfaceGui:FindFirstChild("Layout")
				local Font = x.Display.DisplayPart.SurfaceGui:FindFirstChild("FontUse")
				
				local ToSave = {
				    saveFile = filename,
					saveModel = BoothModel.Value,
					saveBase =  BaseColor.Value,
					saveCounter = CounterColor.Value,
					saveMessage = Message.Value,
					saveImage = ImageID.Value,
					saveLayout = Layout.Value,
					saveFont = Font.Value			
				}
				
				
				local json = HttpService:JSONEncode(ToSave)
				table.insert(CurrentDataList, json)
				
				local FinalSaveData = HttpService:JSONEncode(DataStat)
		
				DataStat.Value =  FinalSaveData
				print(json)

Code above is what I got so far, but again, doesn’t work like I want it to work. game.Rep…Testing is just a string value I’m testing before I mess testing with the Datastore itself

1 Like

I don’t quite understand what you’re asking.

Are you asking how to save multiple objects in a single JSON value?

Because JSON can be arrays, too:

local http = game:GetService("HttpService")

local a = { A = "the quick" }
local b = { B = "brown fox" }
local c = { C = "jumps over" }

local allThree = { a, b, c }

local allThreeJSON = http:JSONEncode(allThree)
local decoded = http:JSONDecode(allThreeJSON)

print(allThreeJSON) -- [{"A":"the quick"},{"B":"brown fox"},{"C":"jumps over"}]
print(decoded) --[[
outputs the original array:
   ▼  {
    [1] =  ▼  {
       ["A"] = "the quick"
    },
    [2] =  ▼  {
       ["B"] = "brown fox"
    },
    [3] =  ▼  {
       ["C"] = "jumps over"
    }
  }
]]

So, I am not understanding what you are doing. Well, CurrentDataList is never used, only saved to.
So what I beleive you are trying to do is replace

local FinalSaveData = HttpService:JSONEncode(DataStat)

to

local FinalSaveData = HttpService:JSONEncode(CurrentDataList)

also, in the

local FinalSaveData = HttpService:JSONEncode(DataStat)

You are trying to encode DataStat, which I believe is an Instance, you cant encode instances in json

My revised code since I think I’m understanding this, but you know how you can do

for I, x in pairs(Something:GetChildren()) do to get individual “Data” of a whole thing, how would I do that with a JSONEncode if there’s no specific “amount”, but say infinite.

What I have right now

	-- player:FindFirstChild("Data_Folder").BoothData
				local DataStat = game.ReplicatedStorage.Testing
				
				
				if DataStat.Value ~= '' then
					local LoadedData = HttpService:JSONDecode(DataStat.Value)
			
					for i, x in pairs(LoadedData) do
						-- somehow load all previous data in a table so I can state it in the FinalSaveData table
					end
						print(LoadedData)
	
                end
				
				
				
				
				
				local nameFile = filename
				local BoothModel = x:FindFirstChild("StandType")
				local BaseColor = x.Base:FindFirstChild("FullColor")
				local CounterColor = x.Counter:FindFirstChild("FullColor")
				local Message = x.Display.DisplayPart.SurfaceGui:FindFirstChild("Message")
				local ImageID = x.Display.DisplayPart.SurfaceGui:FindFirstChild("ImageID")
				local Layout = x.Display.DisplayPart.SurfaceGui:FindFirstChild("Layout")
				local Font = x.Display.DisplayPart.SurfaceGui:FindFirstChild("FontUse")
				
				local ToSave = {
				    saveFile = filename,
					saveModel = BoothModel.Value,
					saveBase =  BaseColor.Value,
					saveCounter = CounterColor.Value,
					saveMessage = Message.Value,
					saveImage = ImageID.Value,
					saveLayout = Layout.Value,
					saveFont = Font.Value			
				}
				
				
		

				
				local AllData = {ToSave}    -- + all other previous data
				local FinalSaveData = HttpService:JSONEncode(AllData)
		
				DataStat.Value =  FinalSaveData
				print(FinalSaveData)



Since in a UI, I want to automatically display all individual data as its own “save slot”, but not sure how I’d decode the JSON like if it was a for I, x in pairs() method

Okay I figured it out, instead I’ll just limit it to 5 slots, it’ll save me the confusion.

I think you might want to take a step back because I think you might be confusing yourself a bit.

Ignore JSON for a second, let’s go back to basics.

There are two kinds of tables:

  • Arrays (lists like { 1, 2, 3, 4 })
  • Dictionaries (like { a = 1, b = "hello" })

Right now you have a single dictionary (that’s ToSave)

But you want more than one. You can store these in an array.

local dictionary1 = { a = 1, b = "hello" } -- simplified version of your ToSave
local dictionary2 = { a = 2, b = "goodbye" } -- some other data ("save slot")
local array = { dictionary1, dictionary2 }

Look how there are now multiple save datas in a single variable:

print(array[1].b) -- hello
print(array[2].b) -- goodbye

And you can loop

for index, data in pairs(array) do
  print("accessing data number", index)
  print("a =", data.a)
  print("b =", data.b)
end
-- accessing data number	1
-- a =	1
-- b =	hello
-- accessing data number	2
-- a =	2
-- b =	goodbye

And you can add more data slots

local new = { a = 3, c = "aufwiedersehen" }
table.insert(array, new)
print(array[#array].c) -- aufwiedersehen

Now we can talk about JSON. Honestly, it’s the easiest part.

JSON is just a way to turn a table (dictionary1, or array) into a single string for saving and loading.

print(http:JSONEncode(dictionary1)) -- a string containing '{"a":1,"b":"hello"}'
print(http:JSONEncode(array)) -- a string containing '[{"a":1,"b":"hello"},{"a":2,"b":"goodbye"},{"a":3,"c":"aufwiedersehen"}]'

See how it gives you a string? Usually you don’t look at it at all. You hand that string to your data store (because data stores only like strings) and ignore what the string (JSON) actually looks like.

Later you can decode the string back into an object.

However, you never interact with the string (JSON) itself. All you do it decode it. If you had encoded it as an array, it will decode as an array, which you can loop through like normal.


Hopefully that rambling helps you understand a bit. Let me know if you have any questions.

2 Likes

That makes more sense! Thank you!