Skip to content

json ​

Encode and decode JSON while preserving arrays, objects, and null values.

Functions ​

json.encode ​

Serializes to a JSON string.

text
json.encode(value)

json.decode ​

Parses a JSON string into Lua values.

text
json.decode(text)

json.null ​

Explicit JSON null sentinel, retained in arrays and objects.

text
json.null

json.is_null ​

Tests that sentinel.

text
json.is_null(value)

json.array ​

Marks a table as an array, including an empty array.

text
json.array([table])

json.object ​

Marks a table as an object, including an empty object.

text
json.object([table])

Unmarked nonempty tables with integer keys must be contiguous arrays starting at 1. Other tables must have string keys; unmarked empty tables encode as {}. Decoded containers preserve their array/object kind. Nil encodes as JSON null, but assigning nil to a Lua table removes the key: use json.null to retain it. Cycles, unsupported userdata, sparse/mixed-key arrays and non-finite numbers are rejected. Integers beyond Lua's signed 64-bit range must be represented as JSON strings. Limits: 1 MiB JSON text, depth 64, and 10,000 values.

lua
local text = json.encode(json.array({1, json.null, 3}))
local values = json.decode(text)
assert(json.is_null(values[2]))
assert(json.encode(json.array()) == "[]")