Syntax
json.decodeSafe(json: string, options?: DecodeOptions): boolean | any | nil | 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 |
|---|---|---|
success | boolean | True when the operation completed successfully. |
decoded | any | nil | Decoded Luau value or decoded byte string. |
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
Parses JSON without throwing and returns success, value, and 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 success (boolean), decoded (any | nil), errorMessage (string | nil), errorPosition (number | nil). Use the returns table to separate successful values from nil results and recoverable errors.
Example
Parse untrusted JSON without raising an error.
local ok, value, message, position = json.decodeSafe('{"ready":true}')
if ok then
print(value.ready)
else
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.