zendframework/zend-json
Convenience utilities for encoding PHP data to JSON and decoding JSON back to native types, including helpers for advanced cases. Note: this Zend Framework repository was abandoned on 2019-12-31; development continues as laminas/laminas-json.
Start by installing the package via Composer: composer require zendframework/zend-json. Though archived and no longer under active development (last release: 3.1.2, 2019), it remains stable for JSON encoding/decoding in legacy Zend Framework or Laminas-migration projects. The primary entry points are Zend\Json\Json::encode() and Zend\Json\Json::decode(). Begin with these static methods to serialize PHP arrays/objects to JSON or parse JSON strings—especially useful when interoperating with older Zend Framework 2/3 apps or when migrating to Laminas where backward compatibility matters.
Key first use case: Use Json::encode($data) to serialize arrays/objects, and Json::decode($json, true) to convert JSON into associative arrays. For pretty-printing, use Json::prettyPrint($json, $indent = 4) to format JSON with consistent spacing (now preserves spaces after commas).
Json::decode($json, true) to convert JSON from external APIs into associative arrays in zf1/zf2-era controllers or models.Zend\Json\Server for JSON-RPC implementations; still relevant in legacy XML-RPC/JSON-RPC servers.Json::encode($value, true) (second param = true) to disable E_STRICT warnings and ensure JSON_HEX tags are used—helpful when outputting JSON in error-prone environments.Json::encode($object) to serialize complex objects (including those with __sleep or custom serialization) without needing json_encode() fallbacks.zendframework/zend-json is fully compatible with laminas/laminas-json as a drop-in replacement.Json::prettyPrint() for human-readable JSON output, now correctly preserving spaces after commas (e.g., {"key": "value", "nested": {"foo": "bar"}} instead of {"key": "value","nested":{"foo":"bar"}}).laminas/laminas-json.{}) in JSON array/object access were causing problems under PHP 7.4. No action is required unless you were explicitly relying on non-standard JSON syntax.Json::decode() with true as second arg is marginally slower than native json_decode() due to extra validation; for high-throughput endpoints, prefer native functions.Json::decode() for untrusted input unless Zend\Json::setEvalMethods(false) (default) is set—prevents accidental code execution via JSONP.Zend\Json\Exception namespaces—ensure your error handling catches Exception\RuntimeException or Exception\InvalidArgumentException as needed.Zend/Json.php paths.Json::prettyPrint() now preserves spaces after commas, avoid relying on this for machine-generated JSON (e.g., APIs) where strict formatting is required.How can I help you explore Laravel packages today?