JSON
Convert between JSON documents and Luau values, validate input, and format saved data. Function pages explain null handling, array and object markers, and encoding options.
Functions
json.arrayMark a new table as a JSON array, especially when the array may be empty. The marker prevents an empty list from turning into {} when it is encoded.
json.cloneCopy 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.
json.decodeParse a complete JSON document into Luau values. Use it after reading a settings file or response body whose syntax you expect to be valid.
json.decodeSafeParse JSON that may be malformed while keeping the error in return values. This is a good choice for a user-selected file or an external response.
json.encodeSerialize a Luau value as compact JSON. Use it before writing structured settings to a file or sending a JSON request body.
json.encodePrettySerialize a value with indentation so a person can inspect or edit the output. It represents the same data as json.encode, with whitespace added between entries.
json.escapeEscape text for the inside of one JSON string token. Use this only when assembling a string token yourself; json.quote and json.encode produce the surrounding quotes for you.
json.formatTake an existing JSON document and produce readable, indented JSON. Use it for inspection or an exported settings file when the source came in compact form.
json.getPathRead a nested table value without writing a chain of nil checks. Use a dot path for ordinary field names, or an explicit key array when a key itself contains a dot.
json.isArrayAsk whether a value is represented as a JSON array. Use it before iterating a decoded value when the input is expected to be a list.
json.isNullDistinguish an explicit JSON null from a missing field. That difference matters in patches where null means “clear this value” and an absent key means “leave it alone.”
json.isObjectCheck whether a table will be treated as a JSON object rather than an array. This is a quick shape check before reading named settings from decoded input.
json.keysCollect the keys of a table into an array. This helps when presenting a list of available settings or inspecting an object with fields chosen at runtime.
json.minifyParse JSON and write it back without optional formatting whitespace. Use it when you want a compact stored or transmitted representation of an existing document.
json.objectMark a shallow copy of a table as a JSON object. This keeps an empty settings record as {} and makes the intended shape clear to a consumer.
json.quoteProduce one complete JSON string token from text. It is handy when you need a quoted value for a small constructed fragment; use json.encode for a whole table or document.
json.typeGet a JSON-aware type label for a value. It distinguishes arrays and objects, which Luau’s ordinary type function both calls tables.
json.unescapeDecode JSON escape sequences from string contents. It accepts either a quoted JSON string token or its contents without the outer quotes.
json.validateCheck whether text is syntactically valid JSON without keeping a decoded value. Use this to accept or reject edited text before storing it.
json.valuesCollect the values of a table into an array. Use it when you need a quick summary of contents but the original keys are not relevant.