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

> Parse JSON string va re-encode thanh readable formatted JSON bang Real JSON parser va encoder.

<div className="real-function-page">
  ```lua theme={null}
  json.format(json: string, encodeOptions?: EncodeOptions, decodeOptions?: DecodeOptions): string
  ```

  ## Đối số

  <ParamField path={"json"} type={"string"} required>
    JSON string duoc parse truoc formatting. Function doc argument nay bang string type checking.
  </ParamField>

  <ParamField path={"encodeOptions"} type={"EncodeOptions"}>
    Optional encode options duoc dua vao re-encode step. json.format doc cung encode options nhu json.encode, sau do ep pretty thanh true.
  </ParamField>

  <ParamField path={"decodeOptions"} type={"DecodeOptions"}>
    Optional decode options duoc dung khi parsing input JSON. boolean dat useNull. table co the dat useNull va maxDepth.
  </ParamField>

  ## Giá trị trả về

  <ResponseField name={"formatted"} type={"string"}>
    Readable formatted JSON string duoc tao sau parsing va re-encoding input.
  </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ự.                       |

  ### DecodeOptions

  Kiểm soát hành vi phân tích cú pháp JSON.

  | Name       | Type      | Required | Mô tả                                                                   |
  | ---------- | --------- | -------- | ----------------------------------------------------------------------- |
  | `useNull`  | `boolean` | No       | Biểu thị JSON null bằng giá trị null của thư viện thay vì con số không. |
  | `maxDepth` | `integer` | No       | Đặt độ sâu lồng tối đa, được kẹp từ 1 đến 4096.                         |

  ## Ví dụ

  Parse existing JSON va re-encode thanh readable formatted JSON.

  ```lua theme={null}
  local compact = '{"b":2,"a":1}'
  local formatted = json.format(compact, {
      pretty = true,
      sortKeys = true,
  }, {
      useNull = true,
  })

  print(formatted)
  ```
</div>
