> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# json.encode

> Serializes a Lua value to a JSON string as JSON. json.stringify is registered to the same function.

Serializes a Lua value to a JSON string as JSON. json.stringify is registered to the same function.

## Syntax

```lua theme={null}
json.encode(value: any, options?: EncodeOptions): string
```

## Arguments

| Name      | Type            | Required | Description                                                                                                                                                                                                                                                                                                 |
| --------- | --------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value`   | `any`           | Yes      | Value to encode. nil, booleans, numbers, strings, tables, and json.null are handled directly. Unsupported Lua types encode as null unless errorOnUnsupported is true.                                                                                                                                       |
| `options` | `EncodeOptions` | No       | Optional encode options. A boolean toggles pretty output. A number enables pretty output with that many spaces clamped to 0.16. A string enables pretty output with that indent. A table can set pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth, and indent. |

## Returns

| Name   | Type     | Description                                                     |
| ------ | -------- | --------------------------------------------------------------- |
| `json` | `string` | Encoded JSON string. Encoding errors are thrown as Luau errors. |

## Types

### EncodeOptions

Controls JSON serialization and formatting.

| Name                         | Type      | Required | Description                                                 |
| ---------------------------- | --------- | -------- | ----------------------------------------------------------- |
| `pretty`                     | `boolean` | No       | Enables formatted, multi-line JSON output.                  |
| `sortKeys`                   | `boolean` | No       | Sorts object keys before encoding.                          |
| `emptyTableAsArray`          | `boolean` | No       | Encodes an empty table as an array instead of an object.    |
| `errorOnUnsupported`         | `boolean` | No       | Raises an error instead of substituting unsupported values. |
| `encodeInvalidNumbersAsNull` | `boolean` | No       | Encodes NaN and infinite numbers as JSON null.              |
| `maxDepth`                   | `integer` | No       | Sets the maximum nesting depth, clamped from 1 to 4096.     |
| `indent`                     | `string`  | No       | Sets the indentation string, limited to 32 characters.      |

## Example

Encode JSON-compatible values, using json.array when an empty array must stay an array.

```lua theme={null}
local payload = {
    name = "Real",
    enabled = true,
    tags = json.array({ "api", "docs" }),
    missing = json.null,
}

local text = json.encode(payload, {
    sortKeys = true,
})

print(text)
```
