A lightweight JSON encode/decode library written in Luau, compatible with Lune runtime without external dependencies.
ujson.encode/ujson.decodesupport- Real JSON
nullviaujson.null - Proper string escaping
- Full JSON object & array support
- Detects:
- circular references
- sparse arrays
- invalid table keys
- Built from scratch parser (no dependencies)
Using Pesde or simply copying the lib.
local ujson = require(path.to.lib)If you use Pesde:
pesde add onefiverdev/ujson
pesde install # remember to install dependencieslocal ujson = require("path.to.lib")
local data = {
hello = "world",
num = 123,
nested = {
ok = true,
value = ujson.null
}
}
print(ujson.encode(data))Output:
{"hello":"world","num":123,"nested":{"ok":true,"value":null}}
local ujson = require("path.to.lib")
local str = [[
{
"hello": "world",
"num": 123
}
]]
local data = ujson.decode(str)
print(data.hello) -- world
print(data.num) -- 123Lua has no native null, so this library uses:
ujson.nullExample:
local t = {
value = ujson.null
}This will serialize as:
{"value":null}
A table is treated as an array if:
- keys are
1..nwithout gaps - no string keys exist
{ "a", "b", "c" }{ a = 1, b = 2 }The library throws errors for:
- circular references
- sparse arrays
- mixed/invalid table keys
- unsupported value types
- invalid JSON during decoding
Encodes a Lua table into a JSON string.
Parses a JSON string into a Lua table.
Represents JSON null.
local x = ujson.nulllocal ujson = require("path.to.lib")
local encoded = ujson.encode({
name = "Luau",
features = { "fast", "lightweight" },
nilValue = ujson.null
})
local decoded = ujson.decode(encoded)
print(encoded)
print(decoded.features[1])