How would I go about unpacking a dictionary into a string

I’m trying to make a dictionary that looks like this, into a string.

{
	["CashierName"] = "croissantDev",
	["Mango "] = {},
	["Pineapple Smoothie"] = {},
	["PlayerToRecieveName"] = "croissantDev",
	["Strawberry and Banana"] = {}
}

Unfortunately when I tried tostring() it just returned table: 0x96e67bb8d2041833

Does anyone have any ideas on how to go about making a dictionary into a string?

You can try HttpService:JSONEncode to do what you want, tostring just returns the memory address that it’s located at,

3 Likes

Would JSONEncode return a string?

Yes it does, it turns it into a JSON encoded string and returns, if you need it back into a dictionary, use JSONDecode

Yeah it will Encode the table into a JSON string format.

-- Services
local HttpsService = game:GetService("HttpsService")

-- Table
local YourTable = {}

-- Encode
local StringOfYourTable = HttpsService:JSONEncode(YourTable)
print(StringOfYourTable) --> String

-- Decode
local Decode  =  HttpsService:JSONDecode(StringOfYourTable)
print(Decode) --> Table

JSONEnocde is definitely the better option but if you want, here is a function I wrote. (untested and written on mobile so there could be typos)

local function serialize(data)
    local strData = ""
    for i, v in pairs(data) do
        strData.Value = strData.Value.."["..i.."] = "..data..", "
    end 
    return strData
end

local function deserialize(strData)
    local data = {}
    for i, v in pairs(strData:split(", ")) do
        local idx = v:split(" = ")[1]
        local val = v:split(" = ")[2]
        data[idx] = val
    end
    return data
end
1 Like

You can also I think use table.concat({table},",")