Skip to content

Quick start

Output modes

Every data-returning method accepts an optional $output argument:

ConstantValueReturns
JsonFast::OUTPUT_ARRAY0PHP array (default)
JsonFast::OUTPUT_STRING1Compact or formatted JSON string
JsonFast::OUTPUT_OBJECT2stdClass 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}'); // true

Analyse — 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 collection

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

Native tools, weird experiments, and practical performance work.