egeloen/json-builder
PHP 5.6+ library to build JSON using Symfony PropertyAccess paths. Set nested values, arrays, and raw/unescaped values while retaining control over escaping. Produces JSON strings from a fluent builder API with strong test coverage.
json_encode() calls and improving maintainability.json_encode() hacks with a battle-tested, Symfony-backed library (leveraging PropertyAccess for path-based manipulation).json_encode() or Response::json().json_encode() is optimized and sufficient for most cases.json_encode() or specialized libraries like Spatie’s Array to JSON."This package standardizes how we generate JSON responses across our Laravel services, reducing bugs and tech debt. It’s like a ‘JSON assembly line’—we can now safely embed custom data (e.g., JS snippets, debug info) without security risks, while keeping the codebase clean. Low risk (MIT license, 100% test coverage), high reward for edge cases like our [specific use case, e.g., dynamic frontend payloads]."
*"Replace scattered json_encode() calls with a fluent, path-based JSON builder that:
setValue('[key]', '<script>...</script>', false) for raw HTML/JS).setValue('[user][profile][0]', 'value')).$builder
->setValues(['user' => ['name' => 'Alice']])
->setValue('[user][script]', '<alert>Hello!</alert>', false) // Raw HTML
->build();
// Output: {"user":{"name":"Alice","script":"<alert>Hello!</alert>"}}
Why? Fewer bugs, reusable logic, and explicit control over JSON output."*
"This package explicitly manages JSON escaping, reducing XSS risks when embedding user-generated content. Unlike json_encode(), it lets us opt-out of escaping for trusted contexts (e.g., admin panels, debug tools) while defaulting to safe behavior. MIT license + 100% test coverage = low risk."
How can I help you explore Laravel packages today?