Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Printer Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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

Implementation Patterns

  • CLI tools & scaffolding: Use Printer in console commands to format JSON fixtures or generated config files (e.g., config/extra.json) with team-specific indentation (spaces vs. tabs).
  • Test utilities: In unit/integration tests, normalize baseline JSON fixtures before comparing outputs to avoid false failures due to whitespace differences.
  • Hybrid workflows: Combine with 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).
  • Custom formatters: Wrap the printer in a reusable service (e.g., JsonFormatterService) that injects indentation preference from config, supporting environment-specific formatting (e.g., compact for prod logs, pretty for dev).

Gotchas and Tips

  • Only indents, no parsing: The printer assumes the input is valid JSON — it does not validate or decode. Pass invalid JSON (e.g., trailing commas) will throw JsonException. Always validate first with json_validate() (PHP ≥8.3) or json_decode(..., true, 512, JSON_THROW_ON_ERROR).
  • Indent robustness: Version ≥2.0.1 rejects mixed tabs/spaces as indent (e.g., '\t '), but allows tab-only or space-only (e.g., ' ' or "\t"). Use ' ' for 4 spaces, "\t" for tabs.
  • Idempotency: The printer is idempotent — applying it twice yields the same result. Safe to use unconditionally in pipelines.
  • Line endings: Since v2.0.0, use the optional third argument to customize newlines (e.g., Printer::print($json, ' ', "\n")), especially for cross-platform compatibility or legacy system expectations (e.g., \r\n).
  • Avoid over-engineering: This isn’t a full formatter — it preserves key formatting choices like space after colons or commas. Stick to it only for indentation consistency; for full formatting control, use json_encode($data, JSON_PRETTY_PRINT) instead.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport