Skip to content

Instantly share code, notes, and snippets.

@otavionetoca
Last active April 23, 2021 16:26
Show Gist options
  • Save otavionetoca/cf52e020c54bdd8ddeecbb6ac702f461 to your computer and use it in GitHub Desktop.
Save otavionetoca/cf52e020c54bdd8ddeecbb6ac702f461 to your computer and use it in GitHub Desktop.
TableToStringifiedJSON
function TableToStringifiedJSON(table)
if type(table) ~= "table" then
Package:Log("[UTILS] Param expected as type table")
return
end
local stringified = ""
for k, _ in pairs(table) do
if type(k) == "table" then
TableToStringifiedJSON(k)
end
stringified = stringified .. "{\"" .. tostring(k) .. "\":{"
for k2, v2 in pairs(table[k]) do
local value
if type(v2) == "string" then
value = "\"" .. tostring(v2) .. "\""
else
value = tostring(v2)
end
stringified = stringified .. "\"" .. tostring(k2) .. "\":" .. value .. ","
end
end
-- Remove last comma so JS is able to parse it
return stringified:sub(0, #stringified - 1) .. "}}"
end
@olivatooo
Copy link

GENIUS SAVED MY LIFE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment