PHPackages                             jsonrepair/jsonrepair - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jsonrepair/jsonrepair

ActiveLibrary[Database &amp; ORM](/categories/database)

jsonrepair/jsonrepair
=====================

Repair invalid JSON documents - A modern PHP 8.4+ port with streaming support. Fixes common issues: missing quotes, trailing commas, comments, single quotes, and more.

v1.0.1(7mo ago)01.7k↓75.2%[1 issues](https://github.com/daniesy/jsonrepair/issues)ISCPHPPHP ^8.4CI passing

Since Oct 31Pushed 7mo agoCompare

[ Source](https://github.com/daniesy/jsonrepair)[ Packagist](https://packagist.org/packages/jsonrepair/jsonrepair)[ Docs](https://github.com/daniesy/jsonrepair-php)[ RSS](/packages/jsonrepair-jsonrepair/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

JSON Repair - PHP 8.4+
======================

[](#json-repair---php-84)

[![Latest Version](https://camo.githubusercontent.com/9bed0fdc96e00533dd2c853e957e0d9ce3598a9d71780da3d57efb293906ecf0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a736f6e7265706169722f6a736f6e7265706169722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jsonrepair/jsonrepair)[![PHP Version](https://camo.githubusercontent.com/f0813eaf7a0c142e092815b51bfc0a263444c029659bd38410085bececb49cc2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a736f6e7265706169722f6a736f6e7265706169722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jsonrepair/jsonrepair)[![License](https://camo.githubusercontent.com/7f7d81e55a4b6dbb7b1515195fb1cd5c9a38fcb77d03bfe0c58887c0e56a67af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a736f6e7265706169722f6a736f6e7265706169722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jsonrepair/jsonrepair)

A modern PHP 8.4+ port of the [jsonrepair](https://github.com/josdejong/jsonrepair) library that repairs invalid JSON documents.

Features
--------

[](#features)

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

Requirements
------------

[](#requirements)

- PHP 8.4 or higher

Installation
------------

[](#installation)

Install via Composer:

```
composer require jsonrepair/jsonrepair
```

For development:

```
git clone https://github.com/daniesy/jsonrepair-php.git
cd jsonrepair-php
composer install
```

Usage
-----

[](#usage)

### Regular (Non-streaming)

[](#regular-non-streaming)

For 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();
}
```

### Streaming

[](#streaming)

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;
}
```

Examples
--------

[](#examples)

Check out the [examples](examples/) directory for practical usage examples:

```
php examples/streaming_example.php
```

Testing
-------

[](#testing)

Run 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-html
```

### Test Results

[](#test-results)

**All 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](TEST_STATUS.md) for detailed test results.

Architecture
------------

[](#architecture)

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

Original Project
----------------

[](#original-project)

This is a PHP port of [jsonrepair](https://github.com/josdejong/jsonrepair) by Jos de Jong.

Contributing
------------

[](#contributing)

Contributions are welcome! Please follow these guidelines:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Write tests for your changes
4. Ensure all tests pass (`composer test`)
5. Commit your changes (`git commit -m 'Add amazing feature'`)
6. Push to the branch (`git push origin feature/amazing-feature`)
7. 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

Credits
-------

[](#credits)

- **Original Author**: [Jos de Jong](https://github.com/josdejong)
- **Original Project**: [jsonrepair](https://github.com/josdejong/jsonrepair)
- **PHP Port**: Dan Florian
- This PHP port maintains feature parity with the JavaScript/TypeScript implementation

License
-------

[](#license)

ISC License (same as original project)

Copyright (c) 2020-2025 by Jos de Jong

See [LICENSE](LICENSE) file for details.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance62

Regular maintenance activity

Popularity20

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

2

Last Release

229d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e4cb151c603cc686b4ba9ac6a6301f5188cbc8ce8625a2c91ff4274ceeb04090?d=identicon)[Daniesy](/maintainers/Daniesy)

---

Tags

streamjsonparsergeneratorstreamingNDJSONparsemongodbinvalidfixbrokenrepairmalformed

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jsonrepair-jsonrepair/health.svg)

```
[![Health](https://phpackages.com/badges/jsonrepair-jsonrepair/health.svg)](https://phpackages.com/packages/jsonrepair-jsonrepair)
```

###  Alternatives

[salsify/json-streaming-parser

A streaming parser for JSON in PHP.

7887.1M21](/packages/salsify-json-streaming-parser)[timgws/query-builder-parser

Build complex Eloquent &amp; QueryBuilder queries automatically when using jQuery-QueryBuilder

161403.9k](/packages/timgws-query-builder-parser)[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

117548.2k6](/packages/apix-cache)[sonata-project/doctrine-mongodb-admin-bundle

Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle

68862.7k3](/packages/sonata-project-doctrine-mongodb-admin-bundle)[icecave/duct

An incremental streaming JSON parser.

773.5k1](/packages/icecave-duct)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
