PHPackages                             philiprehberger/php-safe-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. philiprehberger/php-safe-json

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

philiprehberger/php-safe-json
=============================

Safe JSON parsing with exceptions, schema validation, and typed getters

v1.0.2(4d ago)11MITPHPPHP ^8.2CI passing

Since Mar 13Pushed yesterdayCompare

[ Source](https://github.com/philiprehberger/php-safe-json)[ Packagist](https://packagist.org/packages/philiprehberger/php-safe-json)[ Docs](https://github.com/philiprehberger/php-safe-json)[ RSS](/packages/philiprehberger-php-safe-json/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (4)Used By (0)

PHP Safe JSON
=============

[](#php-safe-json)

[![Tests](https://github.com/philiprehberger/php-safe-json/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-safe-json/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/93802467cde239b348eb0c81ebfd1a94058f048cecb3cc9adce139f5f6a0b25e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d736166652d6a736f6e2e737667)](https://packagist.org/packages/philiprehberger/php-safe-json)[![License](https://camo.githubusercontent.com/46500c54bf74989a5de941cac32ca29709c17148e1e38c3f41b4a244c13917b7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068696c69707265686265726765722f7068702d736166652d6a736f6e)](LICENSE)

Safe JSON parsing with exceptions, schema validation, and typed getters.

Requirements
------------

[](#requirements)

- PHP 8.2+

Installation
------------

[](#installation)

```
composer require philiprehberger/php-safe-json
```

Usage
-----

[](#usage)

### Decoding JSON

[](#decoding-json)

```
use PhilipRehberger\SafeJson\SafeJson;

$obj = SafeJson::decode('{"name":"Alice","age":30,"active":true}');

$obj->string('name');   // "Alice"
$obj->int('age');       // 30
$obj->bool('active');   // true
```

### Dot Notation for Nested Access

[](#dot-notation-for-nested-access)

```
$obj = SafeJson::decode('{"user":{"address":{"city":"Vienna"}}}');

$obj->string('user.address.city'); // "Vienna"
$obj->has('user.address.city');    // true
$obj->has('user.address.zip');     // false
```

### Nested Objects

[](#nested-objects)

```
$obj = SafeJson::decode('{"user":{"name":"Alice"}}');

$user = $obj->object('user');
$user->string('name'); // "Alice"
```

### Safe Decoding (No Exceptions)

[](#safe-decoding-no-exceptions)

```
$obj = SafeJson::tryDecode('{invalid}');
// Returns null instead of throwing

$obj = SafeJson::tryDecode('{"valid":true}');
// Returns JsonObject
```

### Default Values

[](#default-values)

```
$obj = SafeJson::decode('{"name":"Alice"}');

$obj->get('name');              // "Alice"
$obj->get('missing', 'default'); // "default"
$obj->get('missing');           // throws JsonKeyException
```

### Encoding

[](#encoding)

```
$json = SafeJson::encode(['key' => 'value']);
// '{"key":"value"}'

$json = SafeJson::tryEncode($data);
// Returns null on failure instead of throwing
```

### Serialization

[](#serialization)

```
$obj = SafeJson::decode('{"key":"value"}');

$obj->toArray(); // ['key' => 'value']
$obj->toJson();  // '{"key":"value"}'
json_encode($obj); // '{"key":"value"}' (JsonSerializable)
(string) $obj;     // '{"key":"value"}' (Stringable)
```

API
---

[](#api)

### `SafeJson`

[](#safejson)

MethodDescription`decode(string $json): JsonObject`Decode JSON string, throws `JsonDecodeException` on failure`tryDecode(string $json): ?JsonObject`Decode JSON string, returns `null` on failure`encode(mixed $data, int $flags = 0): string`Encode to JSON string, throws on failure`tryEncode(mixed $data, int $flags = 0): ?string`Encode to JSON string, returns `null` on failure### `JsonObject`

[](#jsonobject)

MethodDescription`string(string $key): string`Get string value by key`int(string $key): int`Get integer value by key`float(string $key): float`Get float value by key (accepts integers)`bool(string $key): bool`Get boolean value by key`array(string $key): array`Get array value by key`object(string $key): JsonObject`Get nested JsonObject by key`get(string $key, mixed $default = null): mixed`Get value without type enforcement`has(string $key): bool`Check if key exists`toArray(): array`Return underlying array`toJson(int $flags = 0): string`Return JSON stringAll key-based methods support dot notation for nested access (e.g., `user.address.city`).

### Exceptions

[](#exceptions)

ExceptionThrown When`JsonDecodeException`Invalid JSON input`JsonEncodeException`Failed to encode data`JsonKeyException`Missing key or type mismatchDevelopment
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse
```

License
-------

[](#license)

MIT

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance100

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~2 days

Total

3

Last Release

4d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (7 commits)")

---

Tags

jsonsafeencodedecodeparsetyped

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philiprehberger-php-safe-json/health.svg)

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

###  Alternatives

[yethee/tiktoken

PHP version of tiktoken

1572.8M11](/packages/yethee-tiktoken)[kherge/json

Encodes, decodes, and validates JSON data.

60221.6k5](/packages/kherge-json)[gioni06/gpt3-tokenizer

PHP package for Byte Pair Encoding (BPE) used by GPT-3.

85510.4k6](/packages/gioni06-gpt3-tokenizer)[devium/toml

A PHP encoder/decoder for TOML compatible with specification 1.0.0

3963.2k6](/packages/devium-toml)[pmjones/throwable-properties

Copies properties of a Throwable to a serializable object.

1553.1k1](/packages/pmjones-throwable-properties)[rickselby/nbt

Parser/Writer for the NBT file format

171.2k1](/packages/rickselby-nbt)

PHPackages © 2026

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