JSON

json.pretty

Alias-style helper that serializes a value as formatted JSON.

Syntax

json.pretty(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

Alias-style helper that serializes a value as 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

Pretty-print a Luau value using the encodePretty-compatible alias.

local text = json.pretty({
    category = "JSON",
    functions = 24,
})

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.