json.format Synchronous

Take an existing JSON document and produce readable, indented JSON. Use it for inspection or an exported settings file when the source came in compact form.

Syntax
Luau
json.format(
    text: string,
    options: JsonEncodeOptions?,
    decodeOptions: JsonDecodeOptions?
) -> string

Parameters

Function parameters
ParameterTypeDescription
textstringJSON document to parse and reformat.
optionsJsonEncodeOptions?Optional pretty boolean, indentation width, indentation string, or options table. See Encoding options for all fields.
decodeOptionsJsonDecodeOptions?Optional useNull boolean or options table. By default null becomes json.null and maxDepth is 256.

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.
JsonDecodeOptions fields
Luau
JsonDecodeOptions = boolean | {useNull: boolean?, maxDepth: number?}
FieldDescription
useNullDefaults to true, preserving JSON null as json.null. false converts null to nil, which can remove table entries. A boolean options argument is shorthand for this field.
maxDepthMaximum nesting depth. Defaults to 256; an integer is required and is clamped to 1–4096.

Returns

string

Pretty-printed JSON text. Invalid JSON raises a parse error.

Usage notes

This parses and serializes again, so original whitespace and key order are not preserved unless you request sortKeys.

Example

Example
Luau
local compact = '{"theme":"dark","enabled":true}'
print(json.format(compact, { sortKeys = true }))
Kawaii documentation