json.validate Synchronous

Check whether text is syntactically valid JSON without keeping a decoded value. Use this to accept or reject edited text before storing it.

Syntax
Luau
json.validate(
    text: string,
    options: JsonDecodeOptions?
) -> (true) OR (false, message: string, position: number)

Parameters

Function parameters
ParameterTypeDescription
textstringJSON document to check.
optionsJsonDecodeOptions?Optional useNull boolean or options table. By default null becomes json.null and maxDepth is 256.

Argument fields

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

(true) OR (false, message: string, position: number)

true for valid JSON; otherwise false, an error message, and its position.

Usage notes

Validation does not check application-specific field names or types. Bad argument types still raise an error.

Example

Example
Luau
local ok, message, position = json.validate('{"enabled":true}')
if not ok then print(message, position) end
print(ok) -- true
Kawaii documentation