Module:Sandbox/Vestrian24Bio/JSON table

local cjson = require "cjson"

-- Function to convert JSON table to HTML table
local function jsonToHtmlTable(jsonStr)
    local jsonTable = cjson.decode(jsonStr)
    local html = "<table border='1'>\n"

    -- Function to generate HTML rows
    local function generateRows(tbl)
        for key, value in pairs(tbl) do
            html = html .. "<tr>\n"
            html = html .. "<td>" .. tostring(key) .. "</td>\n"
            if type(value) == "table" then
                html = html .. "<td>\n"
                html = html .. "<table border='1'>\n"
                generateRows(value)
                html = html .. "</table>\n"
                html = html .. "</td>\n"
            else
                html = html .. "<td>" .. tostring(value) .. "</td>\n"
            end
            html = html .. "</tr>\n"
        end
    end

    generateRows(jsonTable)
    html = html .. "</table>\n"
    return html
end

-- Example JSON string
local jsonStr = [[
{
    "name": "John",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    },
    "phones": ["123-4567", "987-6543"]
}
]]

-- Convert JSON to HTML table
local htmlTable = jsonToHtmlTable(jsonStr)
print(htmlTable)