ergebnis/json-printer
Pretty-print and re-indent JSON strings with customizable indentation (spaces or tabs). Ergebnis\Json\Printer\Printer normalizes indentation only—no escaping/unescaping—making it easy to convert minified or differently formatted JSON into a consistent style.
Install the package via Composer: composer require ergebnis/json-printer. The core class is Ergebnis\Json\Printer\Printer. Start with the simplest use case: taking a minified JSON string and normalizing its indentation — for example, converting single-line JSON into human-readable format with 2-space indentation:
$printer = new Printer\Printer();
$printed = $printer->print($json, ' ');
This is ideal for debugging API responses, log output, or pre-commit formatting of configuration files where json_encode(..., JSON_PRETTY_PRINT) isn’t flexible enough (e.g., switching from 4 spaces to tabs).
Printer in console commands to format JSON fixtures or generated config files (e.g., config/extra.json) with team-specific indentation (spaces vs. tabs).json_encode() for controlled formatting — encode data as JSON, then re-print with custom indentation to match style guides (e.g., enforcing 2-space indents across both PHP config and JSON schema files).JsonFormatterService) that injects indentation preference from config, supporting environment-specific formatting (e.g., compact for prod logs, pretty for dev).JsonException. Always validate first with json_validate() (PHP ≥8.3) or json_decode(..., true, 512, JSON_THROW_ON_ERROR).'\t '), but allows tab-only or space-only (e.g., ' ' or "\t"). Use ' ' for 4 spaces, "\t" for tabs.Printer::print($json, ' ', "\n")), especially for cross-platform compatibility or legacy system expectations (e.g., \r\n).json_encode($data, JSON_PRETTY_PRINT) instead.How can I help you explore Laravel packages today?