json
The global JSON encoder and decoder.
A table in every script's environment.
Behavior
The json global converts between Luau values and JSON strings. It accepts
nil, booleans, finite numbers, strings, and tables containing those values.
Nested values are limited to 16 levels.
json.encode serializes a Luau value to a JSON string. Tables with consecutive
integer keys starting at 1 are encoded as arrays; other tables are encoded as
objects.
json.decode parses a JSON string and returns the corresponding Luau value. JSON
objects become tables and JSON arrays become sequential tables.
Fields
array · decode · encode · isNull · kind · null · object
array
json.array(table?, length?) marks a table for array encoding. Keys must be positive integers. Missing entries encode as null. An explicit length preserves trailing null entries.
decode
json.decode(source) parses JSON. Objects and arrays become tables marked with their JSON kind. Invalid JSON causes an error.
encode
json.encode(value) serializes supported Luau values to JSON. Nonfinite numbers and unsupported userdata cause errors. Nested values are limited to 16 levels.
isNull
json.isNull(value) returns whether the value is the JSON null sentinel.
kind
json.kind(value) reports the JSON kind associated with a value or marked table.
null
The sentinel representing JSON null inside tables. Unlike nil, it preserves an object field or array entry.
object
json.object(table?) marks a table for object encoding, including empty tables.
Example
local source = json.encode({ score = 42, tags = { "new", "high-score" } })
local value = json.decode(source)
print(value.score)