PHPackages                             vanilla/garden-jsont - 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. vanilla/garden-jsont

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

vanilla/garden-jsont
====================

A little JSON transformation library.

v1.2(6y ago)161.3k↓50%1[2 PRs](https://github.com/vanilla/garden-jsont/pulls)1MITPHPCI failing

Since Sep 2Pushed 3y ago2 watchersCompare

[ Source](https://github.com/vanilla/garden-jsont)[ Packagist](https://packagist.org/packages/vanilla/garden-jsont)[ RSS](/packages/vanilla-garden-jsont/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (11)Used By (1)

Garden JSON Transform
=====================

[](#garden-json-transform)

[![Packagist Version](https://camo.githubusercontent.com/f8bde44ff17f6574ed65ae9a9c045239a746a7f515a7e6a4d5b10ccff24ab40d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f76616e696c6c612f67617264656e2d6a736f6e742e7376673f7374796c653d666c6174)](https://packagist.org/packages/vanilla/garden-jsont)[![MIT License](https://camo.githubusercontent.com/767c95744aaf632f668a09ad23e5be350b2d72e0671ce03586f570442ad88243/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f76616e696c6c612f67617264656e2d6a736f6e742e737667)](https://github.com/vanilla/garden-jsont/blob/master/LICENSE)[![CI](https://github.com/vanilla/garden-jsont/actions/workflows/ci.yml/badge.svg)](https://github.com/vanilla/garden-jsont/actions/workflows/ci.yml)[![CLA](https://camo.githubusercontent.com/6312e8649680fe6974d3109074b3fb954c0e84f4b9cdde5a90dfa46223999d3c/68747470733a2f2f636c612d617373697374616e742e696f2f726561646d652f62616467652f76616e696c6c612f67617264656e2d6a736f6e74)](https://cla-assistant.io/vanilla/garden-jsont)

A little library that transforms data based on a specification that can be expressed in JSON.

Purpose
-------

[](#purpose)

The purpose of this package is to provide simple data transformation that can be applied to arrays of data like those that come from JSON APIs. The transformations are specified in simple arrays so that an entire transformation specification can also be serialized to JSON, making this a completely data-driven library.

The Transformer Class
---------------------

[](#the-transformer-class)

The heart of this library is the `Transformer` class that takes a transformation spec in its constructor and then uses that spec to tell it how to transform data.

```
$t = new Transformer($spec);
$target = $t->transform($source);
```

The `Transformer` class is also callable so it can be passed as an argument that requires a callback such as `array_map`.

Transformation Specs
--------------------

[](#transformation-specs)

A transformation spec is an associated array that contains all of the keys of the target array, but with values that are JSON references to keys in the source array.

```
{
    "targetKey1": "/sourceKey1",
    "targetKey2": "/sourceKey2/sourceKey2.1"
}
```

You can see from the above, you can reference a nested value using a forward slash to separate nested references.

### Nested Data

[](#nested-data)

For complex target arrays you nest keys by structuring the target like your desired data format:

```
{
    "user": {
        "username": "/username",
        "fullName": "/fullName"
    }
}
```

### Default Values

[](#default-values)

If you reference a key that doesn't exist in the source data then it will be omitted in the transformed data. If you want to provide a default value you can use the following spec:

```
{
    "username": {
        "$ref": "/username",
        "$default": "anonymous"
    }
}
```

If you omit the `$default` value then the default value is assumed to be `null`.

### Transforming arrays with $each

[](#transforming-arrays-with-each)

You can loop through an array and transform each item using `$each` and `$item`.

```
{
  "$each": "/",
  "$item": {
    "name": "username",
    "id": "userID"
  }
}
```

The above would transform an array like this:

```
[
    { "username":  "bot", "userID":  1 },
    { "username":  "dog", "userID":  2 }
]
```

Into this:

```
[
    { "name":  "bot", "id":  1 },
    { "name":  "dog", "id":  2 }
]
```

You can aso specify a transform for the keys in an array using the `$key` attribute. Here is an example spec:

```
{
  "$each": "/",
  "$item": "userID",
  "$key": "username"
}
```

This spec would transform the example from above into this:

```
{
  "bot": 1,
  "dog": 2
}
```

#### Absolute vs. Relative References

[](#absolute-vs-relative-references)

You may have noticed that the references most of the examples all start with a `/` character. This is because they are all *absolute* references.

If you don't use a `/` at the beginning of your reference then you are specifying a *relative* reference. You use relative references in the `$item` spec to refer to items within the loop.

### Literal Values

[](#literal-values)

Sometimes you might want to provide a specific literal value in the result. You can do this with the `$literal` key.

```
{
    "version": {
        "$literal": "1.1"
    }
}
```

Why doesn't this library do enough?
-----------------------------------

[](#why-doesnt-this-library-do-enough)

Some people think this library is flimsy, foolish, and not worth worrying about. But don't you believe it! The initial implementation of this package is intended to provide the minimum amount of functionality to be useful for 90% of problems.

Any sensible features will be added as requirements evolve. However, they must adhere to the core purpose of this package: transformations that can be expressed as plain JSON.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 85.2% 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 ~86 days

Total

3

Last Release

2274d ago

### Community

Maintainers

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

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

---

Top Contributors

[![tburry](https://avatars.githubusercontent.com/u/97432?v=4)](https://github.com/tburry "tburry (23 commits)")[![charrondev](https://avatars.githubusercontent.com/u/1770056?v=4)](https://github.com/charrondev "charrondev (3 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

production

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/vanilla-garden-jsont/health.svg)

```
[![Health](https://phpackages.com/badges/vanilla-garden-jsont/health.svg)](https://phpackages.com/packages/vanilla-garden-jsont)
```

PHPackages © 2026

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