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.

Syntax
Luau
json.clone(value: any, options: JsonEncodeOptions?) -> JsonValue?

Parameters

Function parameters
ParameterTypeDescription
valueanyValue to copy through the JSON representation.
optionsJsonEncodeOptions?Optional pretty flag, indent width, indent string, or options table. See the encoding options below.

Argument fields

JsonEncodeOptions fields
Luau
JsonEncodeOptions = boolean | number | string | {
  pretty: boolean?, sortKeys: boolean?, emptyTableAsArray: boolean?,
  errorOnUnsupported: boolean?, encodeInvalidNumbersAsNull: boolean?,
  maxDepth: number?, indent: string?
}
FieldDescription
prettyAdd indentation and line breaks. encodePretty and format always enable this.
sortKeysSort object keys by their string representations before encoding. Defaults to false.
emptyTableAsArrayEncode an unmarked empty table as [] instead of {}. Explicit json.array/json.object metadata takes precedence.
errorOnUnsupportedRaise for unsupported values or object keys. By default, unsupported values become null and unsupported keys are skipped.
encodeInvalidNumbersAsNullDefaults to true. false makes NaN and infinite numbers raise an error.
maxDepthMaximum nesting depth. Defaults to 256; an integer is required and is clamped to 1–4096. Cyclic tables always raise.
indentIndentation 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.

JsonValue fields
Luau
JsonValue = string | number | boolean | JsonNull | {JsonValue} | {[string]: JsonValue}
JsonNull fields
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

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
Kawaii documentation