Skip to content

Why JsonFast?

Most PHP JSON workflows combine json_decode(), manual array walking, and third-party libraries for repair and schema validation. php_jsonfast consolidates these into a single Rust-backed extension.

Features

  • Analyse invalid JSON — exact error location plus a ranked list of repair flags to apply (not a blind REPAIR_ALL)
  • Unwrap double- or triple-encoded JSON strings recursively, without manual json_decode() loops
  • Repair broken JSON from real-world sources (JavaScript literals, JSONP wrappers, BOM, comments)
  • Format JSON with configurable indentation or compact minification
  • Path access with dot notation and wildcards (user.name, users[*].email)
  • Transform documents with deep merge and structural diff
  • Schema infer, validate, apply defaults, and diff schemas
  • Output modes on every data-returning method: PHP array (default), JSON string, or stdClass object

Compared to the usual PHP stack

ConcernTypical approachJsonFast
"Why did this fail?"Trial-and-error json_decode() or logging raw bytesanalyse() — line, column, repairable, suggested flags
Double-encoded payloadsRepeated json_decode() until not a stringunwrap() with configurable depth
Malformed JSONCustom fixes or extra librariesrepair() with flag bitmask (or flags from analyse())
Nested readsjson_decode() + array walksget(), has(), search(), extract()
SchemaComposer packages (Opis, JustinRainbow, …)validateSchema(), getSchema(), applySchema()
Diff / mergeUserland helpersdiff(), merge(), schemaDiff()
Output shapeRe-encode or cast manuallyOUTPUT_ARRAY, OUTPUT_STRING, OUTPUT_OBJECT

Diagnose before you repair

json_decode() only answers valid or not. When imports, webhooks, or log pipelines fail, you usually need more:

  • Where did parsing break? (analyse() returns line and column.)
  • Can this be fixed automatically? (repairable tells you before mutating the source.)
  • Which repairs apply? Each suggestion includes the flag name, bit value, and a plain description — build a targeted bitmask instead of spraying REPAIR_ALL at everything.

That matters when you want audit trails ("we stripped comments and quoted unquoted keys"), safer ingestion (skip irreparable garbage), or UI that shows users why their upload failed.

Peel double-encoded JSON

A surprisingly common production bug: the outer document decodes fine, but the value you need is still a JSON string. That happens when:

  • An API JSON-encodes a payload that was already a string
  • A column stores json_encode(json_encode($data))
  • A queue or webhook wraps the body again for transport

In PHP that often becomes a fragile loop:

php
while (is_string($decoded)) {
    $decoded = json_decode($decoded, true);
}

unwrap() does that in one native call, up to $maxDepth, with the same output modes as every other method. The REPAIR_DOUBLE_ENCODED flag also handles this inside repair() when fixing broken documents that mix syntax errors with extra encoding layers.

Built on solid foundations

  • Repair logic for common non-standard JSON (not supported by json_decode())
  • Path queries without decoding the full document into PHP first
  • Schema validation without pulling in a separate Composer dependency
  • Consistent output mode control across all methods
  • Lower overhead on large payloads compared to multi-step PHP pipelines
  • Built on serde_json with order-preserving object keys

Native tools, weird experiments, and practical performance work.