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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpgt/json

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

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

 Structured, type-safe, immutable JSON objects.

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

Since Jan 19Pushed 2w 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 2d ago

READMEChangelog (9)Dependencies (12)Versions (22)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

50

—

FairBetter than 95% of packages

Maintenance71

Regular maintenance activity

Popularity28

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 70% 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 ~239 days

Recently: every ~253 days

Total

9

Last Release

74d 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 (28 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![pixeldrift64](https://avatars.githubusercontent.com/u/15195903?v=4)](https://github.com/pixeldrift64 "pixeldrift64 (5 commits)")

---

Tags

data-transfer-objectjsonjson-schemano-aiweb-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

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.5k196.2M3.1k](/packages/composer-composer)[ergebnis/composer-normalize

Provides a composer plugin for normalizing composer.json.

1.1k41.5M2.8k](/packages/ergebnis-composer-normalize)[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.2k28.9M2.4k](/packages/infection-infection)[paycore/openfintech-data

Openfintech data

22110.1k](/packages/paycore-openfintech-data)[oat-sa/tao-core

TAO core extension

66143.7k124](/packages/oat-sa-tao-core)[pantheon-systems/terminus

A command line interface for Pantheon

3391.5M18](/packages/pantheon-systems-terminus)

PHPackages © 2026

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