Syntax
json.validate(json: string, options?: DecodeOptions): boolean | string | nil | number | nilArguments
| Name | Type | Description |
|---|---|---|
json | string | JSON string to validate. The function reads this argument with string type checking. |
options? | DecodeOptions | Optional decode options. A boolean sets useNull. A table can set useNull and maxDepth. maxDepth is clamped from 1 to 4096. |
Returns
| Name | Type | Description |
|---|---|---|
valid | boolean | true when parsing one complete JSON value succeeds. false when parsing fails inside the protected parse block. |
errorMessage | string | nil | Error message on parse failure. The message is prefixed with json.validate and includes a near-position suffix. This return is only present on failure. |
errorPosition | number | nil | Parser byte position from the parser on parse failure. This return is only present on failure. |
Description
Validates a JSON string by parsing one complete JSON value. On success it returns only true. On parse failure it returns false plus parser error details.
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
Check whether a string contains exactly one complete JSON value.
print(json.validate('{"value":42}'))
local valid, message, position = json.validate('{"value":42 trailing')
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.