json.encodePretty Synchronous

  • json.pretty

Serialize a value as indented JSON for readable settings files or console output. Use it when people will inspect or edit the output. It represents the same data as compact encoding, with whitespace added for readability.

Syntax
Luau
json.encodePretty(value: any, options: JsonEncodeOptions?) -> string

Parameters

Function parameters
ParameterTypeDescription
valueanyValue to serialize.
optionsJsonEncodeOptions?Optional pretty flag, indent width, indent string, or options table. See the encoding options below.

Argument fields

JsonEncodeOptions fields
Luau
JsonEncodeOptions = boolean | number | string | {
  pretty: boolean?, sortKeys: boolean?, emptyTableAsArray: boolean?,
  errorOnUnsupported: boolean?, encodeInvalidNumbersAsNull: boolean?,
  maxDepth: number?, indent: string?
}
FieldDescription
prettyAdd indentation and line breaks. encodePretty and format always enable this.
sortKeysSort object keys by their string representations before encoding. Defaults to false.
emptyTableAsArrayEncode an unmarked empty table as [] instead of {}. Explicit json.array/json.object metadata takes precedence.
errorOnUnsupportedRaise for unsupported values or object keys. By default, unsupported values become null and unsupported keys are skipped.
encodeInvalidNumbersAsNullDefaults to true. false makes NaN and infinite numbers raise an error.
maxDepthMaximum nesting depth. Defaults to 256; an integer is required and is clamped to 1–4096. Cyclic tables always raise.
indentIndentation string, truncated to 32 characters. Defaults to two spaces. A numeric options argument selects an indentation width clamped to 0–16 spaces and enables pretty output.

Returns

string

Formatted JSON text. Default indentation is two spaces.

Example

Example
Luau
print(json.encodePretty({ theme = "dark", enabled = true }))
Kawaii documentation