Skip to content

beautify()

Pretty-print JSON with a configurable indent width.

Signature

php
JsonFast::beautify(
    string $json,
    ?int $indent = 4,
    ?int $output = OUTPUT_ARRAY
): mixed

Parameters

NameTypeDefaultDescription
$jsonstringValid JSON text
$indent?int4Spaces per indent level
$output?intOUTPUT_ARRAYOutput mode

Returns

Depends on $output:

ModeResult
OUTPUT_ARRAYParsed PHP structure (re-serialized internally with formatting)
OUTPUT_STRINGFormatted JSON string with the requested indent
OUTPUT_OBJECTstdClass 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

Native tools, weird experiments, and practical performance work.