Skip to content

Instantly share code, notes, and snippets.

@Not-A-Normal-Robot
Created September 23, 2023 18:02
Show Gist options
  • Save Not-A-Normal-Robot/20d7e5b2c2e91f601d6ded17d032ebda to your computer and use it in GitHub Desktop.
Save Not-A-Normal-Robot/20d7e5b2c2e91f601d6ded17d032ebda to your computer and use it in GitHub Desktop.
simple but not foolproof way to convert lua table to json

note: requires love2d. also not foolproof. im using it to convert techmino mode maps into json. input comes from input.lua.

local input = require('input')

function tableToString(t)
    return "{" .. table.concat(t, ",") .. "}"
end
function jsonTable(t)
    local result = {}
    for key, value in pairs(t) do
        if type(value)=='table' then
            table.insert(result, string.format("\"%s\":%s", key, tableToString(jsonTable(value))))
        elseif type(value)=='string' then
            table.insert(result, string.format("\"%s\":\"%s\"", key, value))
        else
            table.insert(result, string.format("\"%s\":%s", key, value))
        end
    end
    return result
end

-- get simple json string
output = tableToString(jsonTable(input))
love.system.setClipboardText(output);
print(output);
love.timer.sleep(1);
love.event.quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment