Skip to content

repair()

Fix malformed JSON using a repair flag bitmask.

Signature

php
JsonFast::repair(
    string $json,
    ?int $flags = REPAIR_ALL,
    ?int $output = OUTPUT_ARRAY
): mixed

Parameters

NameTypeDefaultDescription
$jsonstringMalformed or non-standard JSON
$flags?intREPAIR_ALLBitmask of REPAIR_* constants
$output?intOUTPUT_ARRAYOutput mode

What it fixes

FlagTypical source
REPAIR_BOMFiles saved with UTF-8 BOM
REPAIR_JSONPcallback({...}) wrappers
REPAIR_COMMENTS// and /* */ in config exports
REPAIR_TRAILING_COMMASJavaScript-style trailing commas
REPAIR_DOUBLE_ENCODEDJSON stored as a string inside JSON
REPAIR_UNQUOTED_STRINGSname: Allan style values
REPAIR_SINGLE_QUOTESSingle-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
);
  • analyse() — suggested flags
  • unwrap() — clean double-encoding without other syntax fixes
  • minify() — parse already-valid JSON

Native tools, weird experiments, and practical performance work.