json.decode Synchronous

  • json.parse

Parse a complete JSON document into Luau values. Use it after reading a settings file or response body whose syntax you expect to be valid.

Syntax
Luau
json.decode(text: string, options: JsonDecodeOptions?) -> JsonValue?

Parameters

Function parameters
ParameterTypeDescription
textstringComplete JSON document.
optionsJsonDecodeOptions?Optional useNull boolean or options table. By default null becomes json.null and maxDepth is 256.

Argument fields

JsonDecodeOptions fields
Luau
JsonDecodeOptions = boolean | {useNull: boolean?, maxDepth: number?}
FieldDescription
useNullDefaults to true, preserving JSON null as json.null. false converts null to nil, which can remove table entries. A boolean options argument is shorthand for this field.
maxDepthMaximum nesting depth. Defaults to 256; an integer is required and is clamped to 1–4096.

Returns

JsonValue?

The decoded value. JSON null is json.null by default; with useNull=false it becomes nil.

JsonValue fields
Luau
JsonValue = string | number | boolean | JsonNull | {JsonValue} | {[string]: JsonValue}
JsonNull fields
Luau
JsonNull = typeof(json.null)

Usage notes

Malformed JSON raises an error. The parser checks syntax, not whether your application’s required keys and types are present.

maxDepth is clamped to 1 through 4096.

Example

Example
Luau
local settings = json.decode('{"theme":"dark","scale":1.25}')
if json.isObject(settings) and type(settings.theme) == "string" then
    print(settings.theme)
end
Kawaii documentation