JSON

json.validate

Validates JSON and returns error details when parsing fails.

Syntax

json.validate(json: string, options?: DecodeOptions): boolean | string | nil | number | nil

Arguments

NameTypeDescription
jsonstringJSON string to parse, format, minify, or validate.
options?DecodeOptionsOptional settings table that controls how the operation behaves.

Returns

NameTypeDescription
validbooleanTrue when the JSON string is valid.
errorMessagestring | nilError message returned when loading or parsing fails, otherwise nil.
errorPositionnumber | nilPosition where parsing failed, or nil when no parse error occurred.

Description

Validates JSON and returns error details when parsing fails.

Call it with 2 parameter(s): json, options. The argument table explains which values are required and which ones only refine the behavior.

It returns valid (boolean), errorMessage (string | nil), errorPosition (number | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Validate JSON and report the parser position on failure.

local valid, message, position = json.validate('{"value":42}')

if not valid then
    warn(message, position)
end

Types

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.