Quick start
Output modes
Every data-returning method accepts an optional $output argument:
| Constant | Value | Returns |
|---|---|---|
JsonFast::OUTPUT_ARRAY | 0 | PHP array (default) |
JsonFast::OUTPUT_STRING | 1 | Compact or formatted JSON string |
JsonFast::OUTPUT_OBJECT | 2 | stdClass for JSON objects |
php
$json = '{"name":"Allan","active":true}';
$data = JsonFast::minify($json); // array
$string = JsonFast::minify($json, JsonFast::OUTPUT_STRING); // JSON string
$object = JsonFast::minify($json, JsonFast::OUTPUT_OBJECT); // stdClass
echo $object->name;Validate
php
JsonFast::validate('{"ok":true}'); // trueAnalyse — know what is wrong before you fix it
analyse() is the diagnostic pass. Use it when imports fail, webhooks look almost-JSON, or you do not want to blindly run REPAIR_ALL.
php
$broken = <<<'JSON'
{
// comment
"name": Allan,
}
JSON;
$report = JsonFast::analyse($broken);Example shape:
php
[
'valid' => false,
'repairable' => true,
'error' => [
'message' => '...',
'line' => 3,
'column' => 12,
],
'repairs' => [
[
'flag' => 'REPAIR_COMMENTS',
'bit' => 4,
'description' => 'Removes // and /* */ comments',
],
[
'flag' => 'REPAIR_UNQUOTED_STRINGS',
'bit' => 32,
'description' => 'Quotes unquoted string values',
],
],
]Apply only the suggested flags:
php
$flags = array_reduce(
$report['repairs'],
fn ($mask, $r) => $mask | $r['bit'],
0
);
$fixed = JsonFast::repair($broken, $flags, JsonFast::OUTPUT_STRING);TIP
inspect() is the lighter check when you only need valid/invalid plus error position — no repair suggestions.
Unwrap — double-encoded JSON in one call
If json_decode() gives you a string when you expected an array, the payload is often encoded twice (or more).
php
$fromApi = '"{\"status\":\"ok\",\"count\":3}"';
$data = JsonFast::unwrap($fromApi);
// ['status' => 'ok', 'count' => 3]
$deep = JsonFast::unwrap('"{\"a\":1}"', maxDepth: 5);Use unwrap() for ingestion pipelines; use repair(..., REPAIR_DOUBLE_ENCODED) when double-encoding is mixed with other syntax problems in the same document.
Repair
When you already know the document needs the full treatment:
php
$fixed = JsonFast::repair($broken, JsonFast::REPAIR_ALL, JsonFast::OUTPUT_STRING);Path access
php
$userJson = '{"user":{"name":"Allan","profile":{"city":"Milton Keynes"}}}';
JsonFast::get($userJson, 'user.profile.city'); // Milton Keynes
JsonFast::has($userJson, 'user.missing'); // false
JsonFast::search($userJson, 'users[*].email'); // wildcard collectionMerge and diff
php
$merged = JsonFast::merge(
'{"name":"Allan","settings":{"theme":"dark"}}',
'{"active":false,"settings":{"language":"en"}}'
);
$changes = JsonFast::diff(
'{"name":"Allan","active":true}',
'{"name":"Allan","active":false,"role":"admin"}'
);Schema
php
$json = '{"name":"Allan","active":true,"roles":["admin","user"]}';
$schema = JsonFast::getSchema($json, JsonFast::OUTPUT_STRING);
$result = JsonFast::validateSchema($json, $schema);See Examples and the API reference for full method signatures.
