json.encode

Serializes a value as JSON.

Syntax

Signature
json.encode(Value: any, Pretty: boolean?) -> string

Aliases

  • json.stringify

Parameters

ParameterTypeDescription
ValueanyValue to serialize.
Prettyboolean?Indent the JSON with two spaces when true. Defaults to false.

Returns

TypeDescription
stringJSON text, at most 16 MiB.

Notes

Serializes nil, json.null, booleans, finite numbers, strings, and tables as JSON. A nonempty sequence with only integer keys from 1 through its length becomes an array. Other tables become objects and require string keys. Empty tables become objects unless marked with json.array. Cycles and unsupported values raise errors.

Encoding is limited to 64 nesting levels and 100,000 visited values.

Example

Luau
local Settings = {
    Enabled = true,
    Tags = json.array({"first", "second"}),
    EmptyList = json.array(),
    MissingValue = json.null,
}

local Text = json.encode(Settings, true)
print(Text)
writefile("Examples/Settings.json", Text)