json. clone Synchronous
Copy a value by encoding it as JSON and decoding the result. Use it to make an independent copy of JSON-compatible settings, including nested tables. It is unsuitable for copying callbacks, drawing handles, or other runtime objects.
Luau
json.clone(value: any, options: JsonEncodeOptions?) -> JsonValue?Parameters
| Parameter | Type | Description |
|---|---|---|
value | any | Value to copy through the JSON representation. |
options | JsonEncodeOptions? | Optional pretty flag, indent width, indent string, or options table. See the encoding options below. |
Argument fields
Luau
JsonEncodeOptions = boolean | number | string | {
pretty: boolean?, sortKeys: boolean?, emptyTableAsArray: boolean?,
errorOnUnsupported: boolean?, encodeInvalidNumbersAsNull: boolean?,
maxDepth: number?, indent: string?
}| Field | Description |
|---|---|
pretty | Add indentation and line breaks. encodePretty and format always enable this. |
sortKeys | Sort object keys by their string representations before encoding. Defaults to false. |
emptyTableAsArray | Encode an unmarked empty table as [] instead of {}. Explicit json.array/json.object metadata takes precedence. |
errorOnUnsupported | Raise for unsupported values or object keys. By default, unsupported values become null and unsupported keys are skipped. |
encodeInvalidNumbersAsNull | Defaults to true. false makes NaN and infinite numbers raise an error. |
maxDepth | Maximum nesting depth. Defaults to 256; an integer is required and is clamped to 1–4096. Cyclic tables always raise. |
indent | Indentation string, truncated to 32 characters. Defaults to two spaces. A numeric options argument selects an indentation width clamped to 0–16 spaces and enables pretty output. |
Returns
JsonValue?The decoded copy. Values that JSON cannot represent are handled according to the encoding options.
Luau
JsonValue = string | number | boolean | JsonNull | {JsonValue} | {[string]: JsonValue}Luau
JsonNull = typeof(json.null)Usage notes
This is a JSON round trip. It does not preserve arbitrary Luau object identity, methods, or metatables.
Example
Luau
local original = { appearance = { scale = 1 } }
local copy = json.clone(original)
copy.appearance.scale = 2
print(original.appearance.scale, copy.appearance.scale) -- 1, 2