PHPackages                             jshannon63/jsoncollect - 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. jshannon63/jsoncollect

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

jshannon63/jsoncollect
======================

Supercharge your JSON using collections

v1.2(8y ago)154.9k↓50%1MITPHPPHP &gt;=7.0.0

Since Dec 11Pushed 8y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (2)Versions (4)Used By (1)

[![Build Status](https://camo.githubusercontent.com/ee5f40e132d28682847d9888ee42f3861e22b821db018e71295485b0f5953349/68747470733a2f2f7472617669732d63692e6f72672f6a7368616e6e6f6e36332f6a736f6e636f6c6c6563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jshannon63/jsoncollect)[![StyleCI](https://camo.githubusercontent.com/5ebcf96b5b14390b342f5e581a6f40d3c690a96e7d67a2b0e9c982c3981d6f63/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131333838393537342f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/113889574)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Supercharge your JSON using collections in PHP
==============================================

[](#supercharge-your-json-using-collections-in-php)

The JsonCollect package allows you to surround your JSON objects with the power of collection methods. Making it easy to traverse your data with methods like each(), pluck(), where(), tap(), reduce(), search(), filter(), map(), transform() and many others.

**Framework Agnostic.**

**100% PHPUnit Test Coverage.**

**Heavily dependent on [tightenco/collect](https://github.com/tightenco/collect), [Matt Stauffer's](https://twitter.com/stauffermatt) split of Laravel's Illuminate Collections.**

See the Laravel documentation [here](https://laravel.com/docs/5.5/collections#available-methods) for more on available methods and usage.

Additionally, this package provides customized getters and setters for accessing keyed data elements. Described in more detail below.

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

[](#installation)

```
composer require jshannon63/jsoncollect

```

if installing in the Laravel framework, JsonCollect will depend on the frameworks copy of Illuminate Collections and tightenco/collect will not be required.

Usage
-----

[](#usage)

Supply your data to the JsonCollect constructor. The form of your data can be a JSON String, a stdClass object or an Array. JsonCollect will recursively dive into the deepest depths of your JSON tree and convert everything to collections.

### Injecting your JSON

[](#injecting-your-json)

```
use Jshannon63\JsonCollect\JsonCollect;

$collection = new JsonCollect($json);
```

Note: You can set the recursion depth of JsonCollect by supplying the optional second constructor argument $depth. Default value is 512.

### Working with your JSON collection

[](#working-with-your-json-collection)

JsonCollect provides custom getter and setter methods for your data. Simply call the methods "get" or "set" with the key name appended to the method name to access your data directly to retrieve or to create/update.

```
// to retrieve the element with the key "name"
$collection->getname();

// will set the value of the element with the key "phone"
$collection->setphone('123-456-7890');
```

As mentioned earlier, you should visit the Laravel documentation [here](https://laravel.com/docs/5.5/collections#available-methods)for more on the *"~100 available methods"* and their usage.

Some fun examples:

```
// send an email to all friends
$collection->getfriends()->each(function ($item, $key) use ($mailer,$subject,$body){
    $mailer->sendmail($item->emailaddress,$subject,$body);
});

// total all your invoices
$total = $collection->getinvoices()->pluck('total')->sum();

// update the sales tax rate for all Kentucky stores
$collection->getstores()->where('state','KY')->transform(function ($item, $key) use ($rate) {
    return $item->settaxrate($rate);
});
```

### Starting from scratch with an empty JsonCollect object

[](#starting-from-scratch-with-an-empty-jsoncollect-object)

It is not necessary to provide data to JsonCollect if your goal is to build a new collection of JSON data. Simply "new up" an instance of JsonCollect and begin adding data. Notice how we use ArrayAccess to simplify our code, and to show flexibility we used the custom getter to retrieve the address collection for setting the city.

```
$collection = new JsonCollect();

$collection['names'] = 'John Doe';

// or if you have multi-level data, you may add another JsonCollect

$collection['address'] = new JsonCollect();
$collection['address']->setstreet('123 Fourth Street');
$collection->getaddress()->setcity('Louisville');
$collection['address']->setstate('KY');
$collection['address']->setzip('40201');

// and we can use the collection method dd() to view the contents...

$collection->dd();
```

Which generates the following output from the die-and-dump.

```
array(2) {
  'names' =>
  string(8) "John Doe"
  'address' =>
  class Jshannon63\JsonCollect\JsonCollect#327 (1) {
    protected $items =>
    array(4) {
      'street' =>
      string(6) "123 Fourth Street"
      'city' =>
      string(4) "Louisville"
      'state' =>
      string(5) "KY"
      'zip' =>
      string(3) "40201"
    }
  }
}
```

### Exporting your JSON when needed

[](#exporting-your-json-when-needed)

The following export() method will return a complete JSON string representation of your collection's data. Note that export will accept the standard json\_encode options.

```
$json = $collection->export(JSON_PRETTY_PRINT);
```

Based on the previous example, this is what we would expect to see from our export.

```
{
    "names": "John Doe",
    "address": {
        "street": "123 Fourth Street",
        "city": "Louisville",
        "state": "KY",
        "zip": "40201"
    }
}
```

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

[](#contributing)

If you would like to contribute refer to CONTRIBUTING.md

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95.5% 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 ~33 days

Total

3

Last Release

3004d ago

### Community

Maintainers

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

---

Top Contributors

[![jshannon63](https://avatars.githubusercontent.com/u/19594127?v=4)](https://github.com/jshannon63 "jshannon63 (21 commits)")[![nymo](https://avatars.githubusercontent.com/u/884833?v=4)](https://github.com/nymo "nymo (1 commits)")

---

Tags

phpjsonlaravelcollection

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jshannon63-jsoncollect/health.svg)

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

###  Alternatives

[cerbero/lazy-json

Framework-agnostic package to load JSONs of any dimension and from any source into Laravel lazy collections.

254309.8k1](/packages/cerbero-lazy-json)[armincms/json

A Laravel Nova field.

25149.4k3](/packages/armincms-json)[lazerg/laravel-enum-pro

A powerful PHP enum extension with collection support, random selection, and magic static calls

4319.0k](/packages/lazerg-laravel-enum-pro)[werxe/laravel-collection-macros

Custom Laravel Collection macros.

2625.8k](/packages/werxe-laravel-collection-macros)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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