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

> Parses a JSON string and re-encodes it as readable formatted JSON using Real's JSON parser and encoder.

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

  ## Arguments

  <ParamField path={"json"} type={"string"} required>
    JSON string to parse before formatting. The function reads this argument with string type checking.
  </ParamField>

  <ParamField path={"encodeOptions"} type={"EncodeOptions"}>
    Optional encode options passed to the re-encode step. json.format reads the same encode options as json.encode, then forces pretty to true.
  </ParamField>

  <ParamField path={"decodeOptions"} type={"DecodeOptions"}>
    Optional decode options used while parsing the input JSON. A boolean sets useNull. A table can set useNull and maxDepth.
  </ParamField>

  ## Returns

  <ResponseField name={"formatted"} type={"string"}>
    Readable formatted JSON string produced after parsing and re-encoding the input.
  </ResponseField>

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

  ### DecodeOptions

  Controls JSON parsing behavior.

  | Name       | Type      | Required | Description                                                           |
  | ---------- | --------- | -------- | --------------------------------------------------------------------- |
  | `useNull`  | `boolean` | No       | Represents JSON null with the library's null sentinel instead of nil. |
  | `maxDepth` | `integer` | No       | Sets the maximum nesting depth, clamped from 1 to 4096.               |

  ## Example

  Parse existing JSON and re-encode it as 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>
