Syntax
json.validate(json: string, options?: DecodeOptions): boolean | string | nil | number | nilArguments
| Name | Type | Description |
|---|---|---|
json | string | JSON string to parse, format, minify, or validate. |
options? | DecodeOptions | Optional settings table that controls how the operation behaves. |
Returns
| Name | Type | Description |
|---|---|---|
valid | boolean | True when the JSON string is valid. |
errorMessage | string | nil | Error message returned when loading or parsing fails, otherwise nil. |
errorPosition | number | nil | Position 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)
endTypes
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.