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.

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

Parameters

Function parameters
ParameterTypeDescription
valueanyValue to copy through JSON.
optionsJsonEncodeOptions?Optional pretty boolean, indentation width, indentation string, or options table. See Encoding options for all fields.

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, with new tables for nested arrays and objects.

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. Methods, metatables, arbitrary userdata, and original table identity are not retained; unsupported values follow encoding options.

Example

Example
Luau
local original = { display = { scale = 1 } }
local copy = json.clone(original)
copy.display.scale = 2
print(original.display.scale) -- 1
Kawaii documentation