repair()
Fix malformed JSON using a repair flag bitmask.
Signature
php
JsonFast::repair(
string $json,
?int $flags = REPAIR_ALL,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$json | string | — | Malformed or non-standard JSON |
$flags | ?int | REPAIR_ALL | Bitmask of REPAIR_* constants |
$output | ?int | OUTPUT_ARRAY | Output mode |
What it fixes
| Flag | Typical source |
|---|---|
REPAIR_BOM | Files saved with UTF-8 BOM |
REPAIR_JSONP | callback({...}) wrappers |
REPAIR_COMMENTS | // and /* */ in config exports |
REPAIR_TRAILING_COMMAS | JavaScript-style trailing commas |
REPAIR_DOUBLE_ENCODED | JSON stored as a string inside JSON |
REPAIR_UNQUOTED_STRINGS | name: Allan style values |
REPAIR_SINGLE_QUOTES | Single-quoted strings |
REPAIR_UNQUOTED_KEYS | {key: 1} style keys |
Use analyse() to discover which flags apply before calling repair().
Examples
Full repair:
php
$broken = <<<'JSON'
{
// user profile
"name": Allan,
"active": true,
}
JSON;
$fixed = JsonFast::repair($broken, JsonFast::REPAIR_ALL, JsonFast::OUTPUT_STRING);Targeted repair (from analyse):
php
$flags = JsonFast::REPAIR_COMMENTS | JsonFast::REPAIR_UNQUOTED_STRINGS;
$fixed = JsonFast::repair($broken, $flags);Strip JSONP only:
php
$clean = JsonFast::repair(
'handleResponse({"ok":true})',
JsonFast::REPAIR_JSONP,
JsonFast::OUTPUT_STRING
);