JSON

json.stringify

Serializes a Lua value to a JSON string like json.encode.

Syntax

json.stringify(value: any, options?: EncodeOptions): string

Arguments

NameTypeDescription
valueanyValue to encode. nil, booleans, numbers, strings, tables, and json.null are handled directly. Unsupported Lua types encode as null unless errorOnUnsupported is true.
options?EncodeOptionsOptional encode options. A boolean toggles pretty output. A number enables pretty output with that many spaces clamped to 0.16. A string enables pretty output with that indent. A table can set pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth, and indent.

Returns

NameTypeDescription
jsonstringEncoded JSON string. Encoding errors are thrown as Luau errors.

Description

Serializes a Lua value to a JSON string like json.encode.

Call it with 2 parameter(s): value, options. The argument table explains which values are required and which ones only refine the behavior.

It returns json (string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Encode JSON-compatible values using the same behavior as json.encode.

local text = json.stringify({
    status = "ready",
    count = 3,
    tags = json.array({ "stringify", "alias" }),
    missing = json.null,
}, {
    sortKeys = true,
})

print(text)

Types

EncodeOptions

Controls JSON serialization and formatting.

pretty? boolean Enables formatted, multi-line JSON output.sortKeys? boolean Sorts object keys before encoding.emptyTableAsArray? boolean Encodes an empty table as an array instead of an object.errorOnUnsupported? boolean Raises an error instead of substituting unsupported values.encodeInvalidNumbersAsNull? boolean Encodes NaN and infinite numbers as JSON null.maxDepth? integer Sets the maximum nesting depth, clamped from 1 to 4096.indent? string Sets the indentation string, limited to 32 characters.