JSON

json.getPath

Reads a nested value from a table using a dotted path or path array.

Syntax

json.getPath(object: table, path: string | table, defaultValue?: any): any | nil
Aliases json.getpath

Arguments

NameTypeDescription
objecttableObject, table, or drawing value to inspect or change.
pathstring | tableWorkspace-relative path to the file or folder.
defaultValue?anyFallback value returned when the requested path is missing.

Returns

NameTypeDescription
pathValueany | nilValue at the requested path, or the fallback value.

Description

Reads a nested value from a table using a dotted path or path array.

Call it with 3 parameter(s): object, path, defaultValue. The argument table explains which values are required and which ones only refine the behavior.

It returns pathValue (any | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Read a nested value with a fallback when the path is missing.

local document = {
    user = {
        profile = { name = "Guest" },
    },
}

local name = json.getPath(document, { "user", "profile", "name" }, "Unknown")
print(name)