A modern PHP 8.4+ port of the jsonrepair library that repairs invalid JSON documents.
- Modern PHP 8.4+ with typed properties, constructor promotion, enums, and match expressions
- Streaming support for memory-efficient processing of large JSON files
- 100% test coverage - All 81 tests passing
- Repairs common JSON issues:
- Add missing quotes around keys
- Add missing escape characters
- Add missing commas
- Add missing closing brackets
- Repair truncated JSON
- Replace single quotes with double quotes
- Replace special quote characters
- Strip trailing commas
- Strip comments (
/* */and//) - Strip markdown fenced code blocks
- Strip JSONP notation
- Handle Python constants (None, True, False)
- Concatenate strings
- And much more...
- PHP 8.4 or higher
Install via Composer:
composer require jsonrepair/jsonrepairFor development:
git clone https://github.com/daniesy/jsonrepair-php.git
cd jsonrepair-php
composer installFor regular-sized JSON documents:
use function JsonRepair\jsonrepair;
try {
$json = "{name: 'John'}";
$repaired = jsonrepair($json);
echo $repaired; // {"name": "John"}
} catch (JsonRepair\Utils\JSONRepairError $e) {
echo "Error: " . $e->getMessage();
}For large JSON documents or streams (memory-efficient):
use function JsonRepair\jsonrepairStream;
use function JsonRepair\jsonrepairStreamToString;
// From a file stream
$stream = fopen('large.json', 'r');
foreach (jsonrepairStream($stream) as $chunk) {
echo $chunk; // Process chunks as they're repaired
}
fclose($stream);
// Or get complete result
$stream = fopen('data.json', 'r');
$repaired = jsonrepairStreamToString($stream);
fclose($stream);
// From an array of chunks
$chunks = ["{name: ", "'John'}"];
foreach (jsonrepairStream($chunks) as $chunk) {
echo $chunk;
}Check out the examples directory for practical usage examples:
php examples/streaming_example.phpRun the test suite using PHPUnit:
# Run all tests
composer test
# Run only unit tests
composer test:unit
# Run only streaming tests
composer test:streaming
# Run tests with coverage
composer test:coverage
# Generate HTML coverage report
composer test:coverage-htmlAll 89 tests passing (100% pass rate) ✅
- 74 regular (non-streaming) tests
- 15 streaming tests
All functionality from the original JavaScript/TypeScript library has been successfully ported and verified, including streaming support.
See TEST_STATUS.md for detailed test results.
This port maintains the same dual-implementation architecture as the original:
- Regular (Non-streaming):
JsonRepair\Regular\JsonRepair- Loads entire document into memory, ideal for regular-sized documents - Streaming:
JsonRepair\Streaming\StreamingJsonRepair- Processes JSON in chunks using PHP generators, ideal for large documents or streaming data
The streaming implementation uses:
- InputBuffer: Manages incoming chunks with efficient memory usage
- OutputBuffer: Manages outgoing chunks with automatic flushing
- Stack: Tracks parsing state (objects, arrays, etc.)
- PHP Generators: Provides memory-efficient iteration over large datasets
This is a PHP port of jsonrepair by Jos de Jong.
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
composer test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to:
- Follow PSR-12 coding standards
- Add tests for new functionality
- Update documentation as needed
- Maintain PHP 8.4+ compatibility
- Original Author: Jos de Jong
- Original Project: jsonrepair
- PHP Port: Dan Florian
- This PHP port maintains feature parity with the JavaScript/TypeScript implementation
ISC License (same as original project)
Copyright (c) 2020-2025 by Jos de Jong
See LICENSE file for details.