PHPackages                             phpgt/json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. phpgt/json

ActiveLibrary

phpgt/json
==========

 Structured, type-safe, immutable JSON objects.

v2.2.0(5mo ago)19.6k↑42.3%1[2 issues](https://github.com/PhpGt/Json/issues)[2 PRs](https://github.com/PhpGt/Json/pulls)5MITPHPPHP &gt;=8.1CI passing

Since Jan 19Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/PhpGt/Json)[ Packagist](https://packagist.org/packages/phpgt/json)[ GitHub Sponsors](https://github.com/sponsors/PhpGt)[ RSS](/packages/phpgt-json/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (6)Versions (15)Used By (5)

Structured, type-safe, immutable JSON objects.
==============================================

[](#structured-type-safe-immutable-json-objects)

Built on top of [PHP.Gt/DataObject](https://www.php.gt/dataobject), this repository adds JSON-specific compatibility. The main usage will be via the `JsonObjectBuilder` class that can be used to build a type of `JsonObject` from a JSON string or decoded JSON object (from `json_decode`).

The purpose of using these classes to represent decoded JSON data is to provide a type-safe, immutable interface to the enclosed data.

---

[ ![Build status](https://camo.githubusercontent.com/1fe5502e279efe588a733777524d620de0c541f128ded4536f3928345eba79e8/68747470733a2f2f62616467652e7374617475732e7068702e67742f6a736f6e2d6275696c642e737667)](https://giub.com/PhpGt/Json/actions)[ ![Code quality](https://camo.githubusercontent.com/cd7f641af94c5707271db3433f3e9e52a43f836fe44e047832812af4edec2091/68747470733a2f2f62616467652e7374617475732e7068702e67742f6a736f6e2d7175616c6974792e737667)](https://app.codacy.com/gh/PhpGt/Json)[ ![Code coverage](https://camo.githubusercontent.com/3dc0436cd98aab1de889326ad4d00cb8b86bc3062c773406429eef9a40356540/68747470733a2f2f62616467652e7374617475732e7068702e67742f6a736f6e2d636f7665726167652e737667)](https://app.codecov.io/gh/PhpGt/Json)[ ![Current version](https://camo.githubusercontent.com/20c7192d47717ac5b079523b1493274ac3fddd9fe1cd12e59deaf36852a2d4c4/68747470733a2f2f62616467652e7374617475732e7068702e67742f6a736f6e2d76657273696f6e2e737667)](https://packagist.org/packages/PhpGt/Json)[ ![PHP.Gt/Json documentation](https://camo.githubusercontent.com/5028541cba236738092662b7d1199bb9cfeab70fe7dc9ab67b1b2ff1c51be052/68747470733a2f2f62616467652e7374617475732e7068702e67742f6a736f6e2d646f63732e737667)](https://www.php.gt/json)The abstract `JsonObject` class extends the [`DataObject` base class](https://www.php.gt/dataobject) to represent the root element of a JSON object. In JSON, this may not necessarily be a key-value-pair object.

The following JSON strings can all be successfully decoded:

- `{"type": "key-value-pair"}` - a typical key-value-pair object
- `[{"name": "first"}, {"name": "second"}` - an array of objects
- `0` - an integer
- `1.05` - a floating point
- `false` - a boolean
- `"Today is going to be a good day"` - a string
- `null` - a null

Because of this, the base `DataObject` would be unable to represent the different types of scalar value in a type-safe way. The `JsonObjectBuilder` class returns a new instance of the abstract `JsonObject` class which is one of the following types:

- `JsonKvpObject` - identical features to `DataObject` with type-safe getters for its keys
- `JsonPrimitive` - a representation of the primitive value, further broken down into types `JsonArrayPrimitive`, `JsonBoolPrimitive`, `JsonFloatPrimitive`, `JsonIntPrimitive`, `JsonNullPrimitive` and `JsonStringPrimitive`.

Usage example
-------------

[](#usage-example)

```
use Gt\Json\JsonObjectBuilder;
use Gt\Json\JsonKvpObject;
use Gt\Json\JsonPrimitive\JsonPrimitive;

$response = file_get_contents("https://example.com/details.json");
$builder = new JsonObjectBuilder();
$jsonObject = $builder->fromJsonString($response);

if($jsonObject instanceof JsonKvpObject) {
	$id = $jsonObject->getInt("id");
}
elseif($jsonObject instanceof JsonPrimitive) {
	$id = $jsonObject->getPrimitiveValue();
}

echo "Requested ID is: $id";
```

Fetch API
---------

[](#fetch-api)

Check out the [PHP implementation of the Fetch API](https://www.php.gt/fetch) that uses this library to work with JSON endpoints asynchronously.

Proudly sponsored by
====================

[](#proudly-sponsored-by)

[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)

[![JetBrains logo.](https://camo.githubusercontent.com/b5639e7738c6dfae9fe3f3e20175570b7376ce2577a772e09c25c2d4f14bf86e/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a6574627261696e732e737667)](https://www.jetbrains.com/community/opensource/)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance61

Regular maintenance activity

Popularity28

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 68% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~255 days

Recently: every ~295 days

Total

8

Last Release

160d ago

Major Versions

v0.1.0 → v1.0.02021-03-15

v1.2.1 → v2.0.02025-03-01

PHP version history (2 changes)v0.1.0PHP &gt;=8.0

v1.2.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e42344b91ce4b91ab57875969f67a0a6a48de570a08bc65d673b06b72fd3a3f?d=identicon)[g105b](/maintainers/g105b)

---

Top Contributors

[![g105b](https://avatars.githubusercontent.com/u/358014?v=4)](https://github.com/g105b "g105b (17 commits)")[![pixeldrift64](https://avatars.githubusercontent.com/u/15195903?v=4)](https://github.com/pixeldrift64 "pixeldrift64 (5 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

data-transfer-objectjsonjson-schemaweb-standards

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgt-json/health.svg)

```
[![Health](https://phpackages.com/badges/phpgt-json/health.svg)](https://phpackages.com/packages/phpgt-json)
```

###  Alternatives

[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k26.2M1.8k](/packages/infection-infection)[ergebnis/composer-normalize

Provides a composer plugin for normalizing composer.json.

1.1k37.3M2.1k](/packages/ergebnis-composer-normalize)[humbug/box

Fast, zero config application bundler with PHARs.

1.3k801.5k69](/packages/humbug-box)[pantheon-systems/terminus

A command line interface for Pantheon

3391.5M13](/packages/pantheon-systems-terminus)[php-opencloud/openstack

PHP SDK for OpenStack APIs. Supports BlockStorage, Compute, Identity, Images, Networking and Metric Gnocchi

2292.2M24](/packages/php-opencloud-openstack)[ergebnis/json-normalizer

Provides generic and vendor-specific normalizers for normalizing JSON documents.

8237.5M6](/packages/ergebnis-json-normalizer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
