webignition/json-pretty-print
Pretty-print JSON strings with consistent, readable formatting. Includes a formatter you can embed in tools or CLIs to clean up minified or messy JSON, with sensible indentation and whitespace handling for clearer diffs, logs, and debugging output.
webignition/json-pretty-print package is a lightweight utility for formatting JSON strings into a human-readable format. It fits well in architectures where:
Accept: text/plain requests).php artisan json:pretty-print).json_encode()/json_decode(), so no external libraries or conflicts.try-catch blocks for robustness.Response::json() or augment it? Consider creating a helper trait/macro (e.g., response()->prettyJson()).composer.json and version-lock it.return response()->json($data, 200, [], JSON_PRETTY_PRINT)).PrettyPrintJsonMiddleware to format responses based on headers/roles.php artisan pretty:print file.json).Symfony\Component\Serializer or nunomaduro/collision (for response formatting).symfony/var-dumper offers richer output.JSON_PRETTY_PRINT flag in json_encode).JsonFormatter::prettyPrint()).Response::macro('prettyJson', function ($data) {
return $this->json($data, 200, [], JSON_PRETTY_PRINT);
});
public function handle(Request $request, Closure $next) {
$response = $next($request);
if ($response->headers->get('Content-Type') === 'application/json') {
$response->setContent(json_encode(json_decode($response->content(), true), JSON_PRETTY_PRINT));
}
return $response;
}
composer.json:
"require": {
"webignition/json-pretty-print": "^1.0"
}
composer update and test thoroughly.JsonPrettyPrinter or creating a decorator.Log::debug($prettyJson)).try-catch to handle malformed JSON gracefully.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malformed JSON input | Exception thrown | Validate input with json_decode($json, true) first. |
| Large JSON payloads (>100MB) | Memory exhaustion | Add size limits or stream processing. |
| Package version incompatibility | Breaking changes (unlikely) | Version-lock in composer.json. |
| Overuse in high-traffic endpoints | Increased response time | Restrict to non-critical paths. |
CONTRIBUTING.md or internal docs:
// Example: Pretty-print JSON in a controller
use Webignition\JsonPrettyPrint\JsonPrettyPrinter;
public function debugData() {
$data = ['key' => 'value'];
$pretty = JsonPrettyPrinter::prettyPrint(json_encode($data));
return response()->json(json_decode($pretty, true));
}
How can I help you explore Laravel packages today?