> ## 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

> Serialize Lua value thanh JSON string bang Real JSON encoder. json.stringify duoc dang ky voi cung function.

<div className="real-function-page">
  ```lua theme={null}
  json.encode(value: any, options?: EncodeOptions): string
  ```

  ## Đối số

  <ParamField path={"value"} type={"any"} required>
    Value de encode. nil, booleans, numbers, strings, tables va json.null duoc xu ly truc tiep. Unsupported Lua types duoc encode thanh null tru khi errorOnUnsupported la true.
  </ParamField>

  <ParamField path={"options"} type={"EncodeOptions"}>
    Optional encode options. boolean toggle pretty output. number bat pretty output voi so spaces duoc clamp ve 0.16. string bat pretty output voi indent do. table co the dat pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth va indent.
  </ParamField>

  ## Giá trị trả về

  <ResponseField name={"json"} type={"string"}>
    Encoded JSON string. Encoding errors duoc throw nhu Luau errors.
  </ResponseField>

  ## Kiểu

  ### EncodeOptions

  Kiểm soát việc tuần tự hóa và định dạng JSON.

  | Name                         | Type      | Required | Mô tả                                                         |
  | ---------------------------- | --------- | -------- | ------------------------------------------------------------- |
  | `pretty`                     | `boolean` | No       | Cho phép xuất JSON nhiều dòng, được định dạng.                |
  | `sortKeys`                   | `boolean` | No       | Sắp xếp các khóa đối tượng trước khi mã hóa.                  |
  | `emptyTableAsArray`          | `boolean` | No       | Mã hóa một bảng trống thành một mảng thay vì một đối tượng.   |
  | `errorOnUnsupported`         | `boolean` | No       | Phát sinh lỗi thay vì thay thế các giá trị không được hỗ trợ. |
  | `encodeInvalidNumbersAsNull` | `boolean` | No       | Mã hóa NaN và số vô hạn dưới dạng JSON null.                  |
  | `maxDepth`                   | `integer` | No       | Đặt độ sâu lồng tối đa, được kẹp từ 1 đến 4096.               |
  | `indent`                     | `string`  | No       | Đặt chuỗi thụt lề, giới hạn ở 32 ký tự.                       |

  ## Ví dụ

  Encode JSON-compatible values va dung json.array khi empty array can giu nguyen la 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)
  ```
</div>
