https://github.com/adhocore/php-json-fixer
Fix truncated JSON data
https://github.com/adhocore/php-json-fixer
adhocore fix-json fix-truncated-json hacktoberfest json pad-json php recover-json rectify-json
Last synced: 3 months ago
JSON representation
Fix truncated JSON data
- Host: GitHub
- URL: https://github.com/adhocore/php-json-fixer
- Owner: adhocore
- License: mit
- Created: 2018-07-19T14:11:50.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-01-20T09:20:40.000Z (over 1 year ago)
- Last Synced: 2025-03-27T00:12:02.883Z (3 months ago)
- Topics: adhocore, fix-json, fix-truncated-json, hacktoberfest, json, pad-json, php, recover-json, rectify-json
- Language: PHP
- Homepage:
- Size: 51.8 KB
- Stars: 49
- Watchers: 5
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
## adhocore/json-fixer
PHP library to fix Truncated JSON data by padding contextual counterpart to the end. Works with PHP5.4 or above.
[](https://github.com/adhocore/php-json-fixer/releases)
[](https://travis-ci.com/adhocore/php-json-fixer?branch=master)
[](https://scrutinizer-ci.com/g/adhocore/php-json-fixer/?branch=master)
[](https://codecov.io/gh/adhocore/php-json-fixer)
[](https://styleci.io/repos/141589074)
[](LICENSE)
[](https://twitter.com/intent/tweet?text=Rescue+and+fix+truncated+JSON+data+in+PHP&url=https://github.com/adhocore/php-json-fixer&hashtags=php,json,jsonfixer,fixjson)
[](https://github.com/sponsors/adhocore)- Zero dependency (no vendor bloat).
**It is a work in progress and might not cover all edge cases.** It would be great if you try it out, open some issues or contribute.
## Installation
```bash
composer require adhocore/json-fixer
```## Usage
```php
use Ahc\Json\Fixer;$json = (new Fixer)->fix('{"a":1,"b":2');
// {"a":1,"b":2}$json = (new Fixer)->fix('{"a":1,"b":true,');
// {"a":1,"b":true}$json = (new Fixer)->fix('{"b":[1,[{"b":1,"c"');
// {"b":[1,[{"b":1,"c":null}]]}// For batch fixing, you can just reuse same fixer instance:
$fixer = new Fixer;$fixer->fix('...');
$fixer->fix('...');
// ...
```## Error
If there's error and fixer cant fix the JSON for some reason, it will throw a `RuntimeException`.
You can disable this behavior by passing silent flag (2nd param) to `fix()` in which case original input is returned:```php
(new Fixer)->silent()->fix('invalid');
// 'invalid'(new Fixer)->silent(true)->fix('invalid');
// 'invalid'(new Fixer)->silent(false)->fix('invalid');
// RuntimeException
```## Missing Value
By default missing values are padded with `null`. You can change it passing desired value to `missingValue()`:
```php
// key b is missing value and is padded with `null`
$json = (new Fixer)->fix('{"a":1,"b":');
// {"a":1,"b":null}// key b is missing value and is padded with `true`
$json = (new Fixer)->missingValue(true)->fix('{"a":1,"b":');
// {"a":1,"b":true}// key b is missing value and is padded with `"truncated"`
// Note that you can actually inject a whole new JSON subset as 3rd param
// but that should be a valid JSON segment and is not checked by fixer.
$json = (new Fixer)->missingValue('"truncated"')->fix('{"a":1,"b":');
// {"a":1,"b":"truncated"}
```## Todo
- [ ] Configurable missing value as per context (options)