JSON

json.encodePretty

Serializes a Luau value into readable formatted JSON.

Syntax

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

Arguments

NameTypeDescription
valueanyNew value to write.
options?EncodeOptionsOptional settings table that controls how the operation behaves.

Returns

NameTypeDescription
jsonstringJSON string result.

Description

Serializes a Luau value into readable formatted JSON.

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 a value as readable multi-line JSON.

local text = json.encodePretty({
    name = "Real",
    version = 1,
    features = { "docs", "examples" },
})

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.