json. clone Synchronous
Copy JSON-compatible data by encoding it and decoding the result. Nested tables become new tables, which is useful when one settings copy must be changed without changing another.
Luau
json.clone(value: any, options: JsonEncodeOptions?) -> JsonValue?Parameters
| Parameter | Type | Description |
|---|---|---|
value | any | Value to copy through JSON. |
options | JsonEncodeOptions? | Optional pretty boolean, indentation width, indentation string, or options table. See Encoding options for all fields. |
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, with new tables for nested arrays and objects.
Luau
JsonValue = string | number | boolean | JsonNull | {JsonValue} | {[string]: JsonValue}Luau
JsonNull = typeof(json.null)Usage notes
This is a JSON round trip. Methods, metatables, arbitrary userdata, and original table identity are not retained; unsupported values follow encoding options.
Example
Luau
local original = { display = { scale = 1 } }
local copy = json.clone(original)
copy.display.scale = 2
print(original.display.scale) -- 1