PHPackages                             shiny/json-logic-php - 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. shiny/json-logic-php

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

shiny/json-logic-php
====================

A modern, complete PHP implementation of JsonLogic. 601/601 official tests. Zero dependencies. PHP 8.1+.

v0.1.1(3mo ago)23417↑180%1[2 PRs](https://github.com/luismoyano/shiny-json-logic-php/pulls)MITPHPPHP &gt;=8.1CI passing

Since Mar 13Pushed 2w ago2 watchersCompare

[ Source](https://github.com/luismoyano/shiny-json-logic-php)[ Packagist](https://packagist.org/packages/shiny/json-logic-php)[ Docs](https://github.com/luismoyano/shiny-json-logic-php)[ RSS](/packages/shiny-json-logic-php/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

shiny/json-logic-php ✨
======================

[](#shinyjson-logic-php-)

[![Packagist](https://camo.githubusercontent.com/783c467982294da456feeb0a3d3768cb80ed88395406f406700e8b729f1cf55f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7368696e792f6a736f6e2d6c6f6769632d706870)](https://packagist.org/packages/shiny/json-logic-php)[![PHP](https://camo.githubusercontent.com/6518db1335bf20fdff07253dc6d6d0cec955b5fb6a8ef1382ac6d73687ecc07f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d626c7565)](https://www.php.net)[![License: MIT](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

> **The most compliant PHP implementation of JSON Logic. ✨**

**shiny/json-logic-php** is a **pure PHP**, **zero-dependency** JSON Logic implementation — the only PHP library that passes 100% of the official JSON Logic tests (in stdclass mode), with no external dependencies and PHP 8.1+ compatibility.

This is the PHP port of [shiny\_json\_logic](https://rubygems.org/gems/shiny_json_logic), the most compliant Ruby implementation of JSON Logic.

---

Why shiny/json-logic-php?
-------------------------

[](#why-shinyjson-logic-php)

This library was built as a modern, fully spec-compliant alternative to [`jwadhams/json-logic-php`](https://github.com/jwadhams/json-logic-php), which at the time of writing passes ~69% of the official test suite, has known vulnerabilities in its dependency tree, and has not seen active maintenance since mid-2024.

shiny/json-logic-php is designed as a drop-in replacement.

- ✅ **100% spec-compliant** — the only PHP library that passes all 601 official JSON Logic tests (stdclass mode).
- 🧩 **Zero runtime dependencies** — stdlib only. Just plug &amp; play.
- 🕰️ **PHP 8.1+** compatible.
- 🔧 **Actively maintained** and aligned with the evolving JSON Logic specification.
- 🔁 **Drop-in aliases**: `JsonLogic` and `JSONLogic` available out of the box.

---

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

[](#installation)

```
composer require shiny/json-logic-php
```

---

Usage
-----

[](#usage)

```
use ShinyJsonLogic\ShinyJsonLogic;

// Basic evaluation
ShinyJsonLogic::apply(['==' => [1, 1]]);
// → true

// With data
ShinyJsonLogic::apply(['var' => 'name'], ['name' => 'Luis']);
// → "Luis"

// Feature flag example
$rule = ['==' => [['var' => 'plan'], 'premium']];
$data = ['plan' => 'premium'];
ShinyJsonLogic::apply($rule, $data);
// → true

// Nested access
ShinyJsonLogic::apply(['var' => 'user.age'], ['user' => ['age' => 30]]);
// → 30
```

### Nested logic

[](#nested-logic)

Rules can be nested arbitrarily:

```
$rule = [
    'if' => [
        ['var' => 'financing'],
        ['missing' => ['apr']],
        []
    ]
];

ShinyJsonLogic::apply($rule, ['financing' => true]);
// → ["apr"]
```

### Drop-in aliases

[](#drop-in-aliases)

`JsonLogic` and `JSONLogic` are available as aliases:

```
JsonLogic::apply(['>' => [['var' => 'score'], 90]], ['score' => 95]);
// → true
```

### Working with `json_decode`

[](#working-with-json_decode)

In PHP, `json_decode` parses a JSON string into either stdclass objects or associative arrays:

```
json_decode('{"var": "name"}');        // → stdClass { $var: "name" }
json_decode('{"var": "name"}', true);  // → ["var" => "name"]
```

Both modes work with this library:

```
// stdclass mode (recommended — full 601/601 compliance)
$rule = json_decode('{"var": "name"}');
$data = json_decode('{"name": "Luis"}');
ShinyJsonLogic::apply($rule, $data);
// → "Luis"

// arrays mode also works (600/601 — see Compatibility)
$rule = json_decode('{"var": "name"}', true);
$data = json_decode('{"name": "Luis"}', true);
ShinyJsonLogic::apply($rule, $data);
// → "Luis"
```

---

Supported operators
-------------------

[](#supported-operators)

CategoryOperatorsData access`var`, `missing`, `missing_some`, `exists`, `val` ✨Logic`if`, `?:`, `and`, `or`, `!`, `!!`Comparison`==`, `===`, `!=`, `!==`, `>`, `>=`, ` [1, 2]], []);
// → throws ShinyJsonLogic\Errors\UnknownOperator

// You can use try/throw for controlled error handling within rules
$rule = [
    'try' => [
        ['throw' => 'Something went wrong'],
        ['cat' => ['Error: ', ['var' => 'type']]]
    ]
];
ShinyJsonLogic::apply($rule, []);
// → "Error: Something went wrong"
```

Exception classes:

- `ShinyJsonLogic\Errors\UnknownOperator` — unknown operator in rule
- `ShinyJsonLogic\Errors\InvalidArguments` — invalid arguments to operator
- `ShinyJsonLogic\Errors\NotANumber` — NaN result in numeric operation

Or catch `ShinyJsonLogic\Errors\Base` to handle all library errors in one sweep.

---

Compatibility
-------------

[](#compatibility)

Tested against the [official JSON Logic test suite](https://github.com/json-logic/.github/tree/main/tests) (601 tests).

ModePassedNotes**stdclass** (`json_decode` without `true`)**601 / 601**Full compliance**arrays** (`json_decode` with `true`)**600 / 601**One PHP-language limitation (see below)### The arrays mode edge case

[](#the-arrays-mode-edge-case)

In PHP, `json_decode('{}', true)` returns `[]` — an empty array, indistinguishable from `json_decode('[]', true)`. This means that in arrays mode, the engine cannot tell an empty object from an empty array.

The one failing case is the `+` operator with an empty object passed directly as the operand list:

```
// stdclass mode — throws NotANumber ✅
ShinyJsonLogic::apply(json_decode('{"+" : {}}'));

// arrays mode — returns 0 instead of NaN ❌
ShinyJsonLogic::apply(json_decode('{"+" : {}}', true));
```

In arrays mode, `{"+" : {}}` is decoded as `["+" => []]` — zero operands — so `+` returns `0` instead of NaN. In stdclass mode, `{}` is preserved as an `EmptyObject` sentinel, and the operator correctly produces NaN.

This is a PHP language limitation, not a bug in this library. The issue has been [reported to the json-logic org](https://github.com/orgs/json-logic/discussions/48).

For this reason, if you need full spec compliance or interoperability with other JSON Logic implementations, we strongly recommend using stdclass mode (`json_decode` without `true`).

All other 600 tests pass in both modes.

---

Development
-----------

[](#development)

```
composer install
./vendor/bin/phpunit
```

To run the official test suite against live test data:

```
./run-official-tests.sh
```

Requires Docker and curl. Fetches the official tests at runtime from `github.com/json-logic/.github`.

---

Contributing
------------

[](#contributing)

Contributions are welcome — especially:

- spec alignment improvements
- missing operators
- edge-case tests

Please include tests with any change.

Repository:

---

Related projects
----------------

[](#related-projects)

- [shiny\_json\_logic (Ruby)](https://rubygems.org/gems/shiny_json_logic) — 601/601 official tests, the most compliant Ruby implementation
- [jsonlogicruby.com](https://jsonlogicruby.com) — JSON Logic playground, docs, and specification reference

---

License
-------

[](#license)

MIT License.

Use it. Fork it. Ship it. (:

---

> Shine bright like a 🐘

###  Health Score

43

—

FairBetter than 90% of packages

Maintenance90

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 77.8% 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 ~3 days

Total

2

Last Release

98d ago

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

v0.1.1PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a5c3f2a3405d1d33d1ca5676455155532d1fd962f7cbde0a2e0a81ca079af41?d=identicon)[luismoyano](/maintainers/luismoyano)

---

Top Contributors

[![luismoyano](https://avatars.githubusercontent.com/u/39064112?v=4)](https://github.com/luismoyano "luismoyano (21 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")

---

Tags

jsonfeature-flagsbusiness-rulesrule-enginerules enginejson-logicjsonlogic

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shiny-json-logic-php/health.svg)

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

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k328.4M737](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k493.5M159](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k139.8M905](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k91.4M664](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30424.1M51](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15677.1M25](/packages/clue-ndjson-react)

PHPackages © 2026

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