Skip to content
Greg Bowler edited this page Apr 21, 2026 · 9 revisions

This library provides a consistent way to read, inspect and construct JSON data in PHP without falling back to loosely typed arrays and stdClass objects.

It builds on PHP.GT/DataObject by adding JSON-specific functionality, so the JSON objects we work with are immutable and expose type-safe getters such as getString() and getInt().

What this library provides

  • JSONObjectBuilder, which turns JSON strings, decoded JSON values and files into JSONObject instances
  • JSONKvpObject for JSON objects with named properties
  • JSONPrimitive subtypes for root JSON values such as strings, numbers, booleans, null and arrays
  • JSONDocument, which helps build JSON responses and nested keys
  • Validator, ValidationSuccess and ValidationError for JSON Schema validation

Where this library fits in

This package can be used on its own in any PHP project.

If you are using WebEngine, the integration point to know about is JSONDocument. WebEngine uses it as the view model for JSON responses, so page logic can call methods like set() and error() and let the framework output the final document.

A minimal example

use GT\Json\JSONObjectBuilder;
use GT\Json\JSONKvpObject;
use GT\Json\JSONPrimitive\JSONPrimitive;

$builder = new JSONObjectBuilder();
$json = $builder->fromJsonString('{"id": 105, "tags": ["php", "json"]}');

if($json instanceof JSONKvpObject) {
	echo $json->getInt("id"), PHP_EOL;
	echo implode(", ", $json->getArray("tags", "string")), PHP_EOL;
}
elseif($json instanceof JSONPrimitive) {
	var_dump($json->getPrimitiveValue());
}

JSONObjectBuilder decides which JSON type has been decoded and returns the matching object. We can then read the data through getters rather than manually checking each value ourselves.


Start with Getting started to install the package and decode a few different kinds of JSON.

Clone this wiki locally