JSON

json.decodeSafe

Parses JSON without throwing and returns success, value, and error details.

Syntax

json.decodeSafe(json: string, options?: DecodeOptions): boolean | any | nil | 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
successbooleanTrue when the operation completed successfully.
decodedany | nilDecoded Luau value or decoded byte string.
errorMessagestring | nilError message returned when loading or parsing fails, otherwise nil.
errorPositionnumber | nilPosition 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)
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.