> ## 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 เป็น JSON string ด้วย Real JSON encoder. json.stringify ถูก register ไปยัง function เดียวกัน

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

  ## Arguments

  <ParamField path={"value"} type={"any"} required>
    Value ที่จะ encode. nil, booleans, numbers, strings, tables และ json.null ถูกจัดการโดยตรง. Unsupported Lua types จะถูก encode เป็น null เว้นแต่ errorOnUnsupported เป็น true.
  </ParamField>

  <ParamField path={"options"} type={"EncodeOptions"}>
    Optional encode options. boolean toggle pretty output. number เปิด pretty output ด้วยจำนวน spaces ที่ clamp เป็น 0.16. string เปิด pretty output ด้วย indent นั้น. table สามารถตั้ง pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth และ indent.
  </ParamField>

  ## Returns

  <ResponseField name={"json"} type={"string"}>
    Encoded JSON string. Encoding errors จะ throw เป็น Luau errors.
  </ResponseField>

  ## Types

  ### EncodeOptions

  ควบคุมการทำให้เป็นอนุกรมและการจัดรูปแบบ JSON

  | Name | Type | Required | คำอธิบาย |
  | - | - | - | - |
  | `pretty` | `boolean` | No | เปิดใช้งานเอาต์พุต JSON แบบหลายบรรทัดที่จัดรูปแบบแล้ว |
  | `sortKeys` | `boolean` | No | เรียงลำดับออบเจ็กต์คีย์ก่อนการเข้ารหัส |
  | `emptyTableAsArray` | `boolean` | No | เข้ารหัสตารางว่างเป็นอาร์เรย์แทนที่จะเป็นวัตถุ |
  | `errorOnUnsupported` | `boolean` | No | ทำให้เกิดข้อผิดพลาดแทนที่จะแทนที่ค่าที่ไม่รองรับ |
  | `encodeInvalidNumbersAsNull` | `boolean` | No | เข้ารหัส NaN และจำนวนอนันต์เป็น JSON null |
  | `maxDepth` | `integer` | No | ตั้งค่าความลึกของการซ้อนสูงสุด โดยยึดไว้ตั้งแต่ 1 ถึง 4096 |
  | `indent` | `string` | No | ตั้งค่าสตริงการเยื้อง โดยจำกัดไว้ที่ 32 อักขระ |

  ## ตัวอย่าง

  Encode JSON-compatible values โดยใช้ json.array เมื่อ empty array ต้องคงเป็น 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>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.