beautify()
Pretty-print JSON with a configurable indent width.
Signature
php
JsonFast::beautify(
string $json,
?int $indent = 4,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$json | string | — | Valid JSON text |
$indent | ?int | 4 | Spaces per indent level |
$output | ?int | OUTPUT_ARRAY | Output mode |
Returns
Depends on $output:
| Mode | Result |
|---|---|
OUTPUT_ARRAY | Parsed PHP structure (re-serialized internally with formatting) |
OUTPUT_STRING | Formatted JSON string with the requested indent |
OUTPUT_OBJECT | stdClass for objects, arrays for JSON arrays |
TIP
OUTPUT_STRING on beautify() is the only case where string output keeps custom indentation. Other methods return compact JSON strings.
Examples
Formatted string for files or logs:
php
$pretty = JsonFast::beautify($json, indent: 2, output: JsonFast::OUTPUT_STRING);
file_put_contents('config.json', $pretty);Tab-width style (8 spaces):
php
echo JsonFast::beautify($compact, indent: 8, output: JsonFast::OUTPUT_STRING);Parse and work with data:
php
$data = JsonFast::beautify($json); // array, default output