JSON

json.tryDecode

Attempts to parse JSON and returns structured error information instead of throwing.

Syntax

json.tryDecode(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

Attempts to parse JSON and returns structured error information instead of throwing.

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

Attempt to decode JSON without throwing on malformed input.

local ok, value, message, position = json.tryDecode('{"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.