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

analyse() is the diagnostic pass. Use it when imports fail, webhooks look almost-JSON, or you need line and column for logging or UI.

php
$broken = <<<'JSON'
{
    // comment
    "name": Allan,
}
JSON;

$report = JsonFast::analyse($broken);

Example shape:

php
[
    'valid' => false,
    'error' => [
        'message' => '...',
        'line' => 3,
        'column' => 12,
    ],
]

Log or surface the error:

php
if (!$report['valid']) {
    logger()->warning('JSON parse failed', [
        'line' => $report['error']['line'],
        'column' => $report['error']['column'],
        'message' => $report['error']['message'],
    ]);
}

TIP

inspect() is the lighter check when you only need valid/invalid plus error position — same error shape, different name.

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 when the outer syntax is valid but the payload is still a JSON 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.