Syntax
json.format(json: string, encodeOptions?: EncodeOptions, decodeOptions?: DecodeOptions): stringArguments
| Name | Type | Description |
|---|---|---|
json | string | JSON string to parse, format, minify, or validate. |
encodeOptions? | EncodeOptions | Optional encoding settings such as pretty output or key ordering. |
decodeOptions? | DecodeOptions | Optional parsing settings used while reading JSON. |
Returns
| Name | Type | Description |
|---|---|---|
formatted | string | Formatted JSON string. |
Description
Parses and re-encodes JSON to normalize formatting.
Call it with 3 parameter(s): json, encodeOptions, decodeOptions. The argument table explains which values are required and which ones only refine the behavior.
It returns formatted (string). Use the returns table to separate successful values from nil results and recoverable errors.
Example
Normalize existing JSON into a readable representation.
local compact = '{"b":2,"a":1}'
local formatted = json.format(compact, {
pretty = true,
sortKeys = true,
})
print(formatted)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.DecodeOptions
Controls JSON parsing behavior.
useNull? boolean Represents JSON null with the library's null sentinel instead of nil.maxDepth? integer Sets the maximum nesting depth, clamped from 1 to 4096.