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.arrayCopy the sequence portion of a table and mark the result for JSON array encoding. This is useful when an empty collection must serialize as []. Use the marker when a consumer expects a list even when it contains no items. Adding elements to the returned table keeps that intended representation.
json.cloneCopy 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.
json.decodeParse JSON text into Luau values. Use json.decodeSafe when a malformed document should return an error result instead of raising. Use it to load settings or interpret an HTTP response body. Parsing checks JSON syntax; check the resulting value and required fields before using them.
json.decodeSafeParse JSON with an explicit success result so a script can handle malformed input without a parsing exception. Use it for user-supplied files or responses that may not contain valid JSON. Read the success flag first, since a valid JSON value can itself be false or null.
json.encodeSerialize a Luau value into JSON text, with options for formatting, key order, unsupported values, and nesting depth. Use it before saving a table with writefile or sending a JSON request body. The options let you choose compact storage or stable, readable output.
json.encodePrettySerialize a value as indented JSON for readable settings files or console output. Use it when people will inspect or edit the output. It represents the same data as compact encoding, with whitespace added for readability.
json.escapeEscape text for inclusion inside a JSON string without adding surrounding quotation marks. Use it only when assembling the contents of a string token yourself. For an entire value or document, json.quote or json.encode handles the surrounding syntax.
json.formatParse an existing JSON document and serialize it again with readable indentation. Use it to make pasted or downloaded JSON easier to inspect. Since this parses and re-encodes the document, it is not a formatting operation that preserves its original spelling.
json.getPathRead a nested table value using a dot-separated path or an array of explicit keys. Use it to read optional nested settings without a chain of nil checks. Pass explicit keys when a field name itself contains a dot.
json.isArrayCheck whether the JSON library classifies a value as an array. Use it to check that parsed input is a list before processing its elements. Explicit metadata takes priority; an unmarked empty table is classified as an object.
json.isNullTest whether a value is the json.null singleton used to preserve JSON null values. Use it to distinguish an explicit null in a document from a missing field. This preserves the difference between “clear this value” and “no value supplied.”
json.isObjectCheck whether the JSON library classifies a value as an object. Use it to check that parsed input is an object before reading named settings. It recognizes marked objects and tables that are not classified as arrays.
json.keysCollect the keys of a table into an array. Use it to build a list of available setting names. Sort the returned array yourself when display order matters.
json.minifyParse JSON text and serialize it again without optional whitespace. Use it to remove formatting from JSON before storing or transferring it. String contents are preserved; only the serialized representation is rebuilt.
json.objectCreate a shallow copy of a table and mark it for JSON object encoding. Use it to make an empty object serialize as {}. Use the marker for a dictionary or settings record whose empty form must remain an object.
json.quoteEscape text and enclose it in quotation marks to produce a JSON string token. Use it to produce one complete string value with correct escaping. A table or a full document should go through json.encode.
json.typeGet the JSON library type label for the first supplied value. Use it when validating JSON-shaped data and you need to distinguish arrays, objects, and null. Other values retain their Luau type labels.
json.unescapeDecode JSON string escapes from quoted or unquoted text. Use it to restore text received as escaped JSON string contents. For a complete JSON document, use json.decode instead.
json.validateCheck whether text is valid JSON without returning its decoded contents. Use it when you only need to accept or reject a document, such as before saving user-edited text. It checks syntax, not the names or types of application-specific fields.
json.valuesCollect the values of a table into an array. Use it when you need the contents of a table without its keys, such as collecting values for a summary. Separate keys and values calls should not be treated as an ordered pair of arrays.