Create HTML template based on Lua table

#!/usr/bin/lua
local json = require "luci.json"
a = '[{"Key1":"value1_1","Key2":"value1_2","Key3":null,"Key4":null},{"Key1":"value2_1","Key2":"value2_2","Key3":"value2_3","Key4":"value2_4"}]'
local decode = json.decode(a)
print (decode)

1st problem faced was : Module not found error for luci.json. LuCI folder had jsonc.so , but not json.lua file and hence could not execute the file successfully.

I have copied the json.lua from git and solved that for the time being. Is this the right approach?

Secondly, decode now has the JSON object converted to table. I would like to create a HTML template based on the table received. Any existing Lua modules that can be used for this purpose?

Folks, Any help in this regard is highly appreciated.

#!/usr/bin/env lua

local json = require "luci.jsonc"

a = '[{"Key1":"value1_1","Key2":"value1_2","Key3":null,"Key4":null},{"Key1":"value2_1","Key2":"value2_2","Key3":"value2_3","Key4":"value2_4"}]'

local data = json.parse(a)
assert(type(data) == "table" and #table > 0, "Invalid JSON data")

local headings = {}
local i, j

for i, j in pairs(data[1]) do
    headings[#headings+1] = i
end

table.sort(headings)

print("<table>")

print("  <tr>")
for i = 1, #headings do
  print("    <th>" .. headings[i] .. "</th>")
end
print("  </tr>")

for i = 1, #data do
  print("  <tr>")
  for j = 1, #headings do
    print("    <td>" .. (data[i][headings[j]] and tostring(data[i][headings[j]]) or "<em>empty</em>") .. "</td>")
  end
  print("  </tr>")
end

print("</table>")