JSON

json.decode

Parses a JSON string with Real's JSON parser and returns the decoded Lua value, marking decoded arrays and objects for the JSON helpers.

Syntax

json.decode(json: string, options?: DecodeOptions): any

Arguments

NameTypeDescription
jsonstringJSON text to parse. The parser accepts one JSON value with optional surrounding whitespace and throws when invalid syntax or trailing non-whitespace characters are found.
options?DecodeOptionsOptional decode options. A boolean sets useNull directly. A table can set useNull and maxDepth; maxDepth is clamped from 1 to 4096.

Returns

NameTypeDescription
decodedanyDecoded value. JSON objects and arrays become tables with JSON type markers, strings become Lua strings, numbers become Lua numbers, booleans become booleans, and null becomes json.null unless useNull is false.

Description

Parses a JSON string with Real's JSON parser and returns the decoded Lua value, marking decoded arrays and objects for the JSON helpers.

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 decoded (any). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Decode JSON into Lua values while preserving JSON array, object, and null markers.

local value = json.decode('{"name":"Real","tags":["docs"],"missing":null}')

print(value.name)
print(value.tags[1])
print(json.type(value))
print(json.isNull(value.missing))

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.