PHPackages                             iteks/laravel-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. iteks/laravel-json

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

iteks/laravel-json
==================

A Laravel package for simplified JSON data manipulation, offering seamless conversion to collections or arrays with attribute filtering.

v1.2.2(1mo ago)417.6k↑15.7%MITPHPPHP ^8.1

Since Feb 16Pushed 1y agoCompare

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

READMEChangelog (4)Dependencies (12)Versions (7)Used By (0)

[![Laravel JSON](https://raw.githubusercontent.com/iteks/art/master/logo-packages/laravel-json.svg)](https://raw.githubusercontent.com/iteks/art/master/logo-packages/laravel-json.svg)

[![Total Downloads](https://camo.githubusercontent.com/2e31d2b8553cd5e1160a6fc32cc4b548b68cedf240003c1f79bb54d4195f81c7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6974656b732f6c61726176656c2d6a736f6e)](https://packagist.org/packages/iteks/laravel-json)[![Latest Stable Version](https://camo.githubusercontent.com/41675601eea4842af786b5a3f9de13d47f535bb82f273f524c280d810b855f80/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6974656b732f6c61726176656c2d6a736f6e)](https://packagist.org/packages/iteks/laravel-json)[![License](https://camo.githubusercontent.com/a42cd1daad18f3ca1f453c8812b2c2b4e4660bb087e7a7e8fd7e00eac4aa61ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6974656b732f6c61726176656c2d6a736f6e)](https://packagist.org/packages/iteks/laravel-json)

The **Laravel JSON** package is a powerful and versatile tool designed to enhance the handling of JSON data within Laravel applications. With its intuitive API, developers can effortlessly convert JSON files into Laravel collections or associative arrays, facilitating easy data manipulation and access. Whether you're dealing with configuration files, dataset imports, or any JSON-formatted data source, this package simplifies the process, allowing you to focus on building feature-rich applications. Built with flexibility in mind, it supports optional attribute filtering, enabling precise data retrieval tailored to your needs. Additionally, enforce any JSON model column's data structure with column definitions, ensuring JSON data consistency with every interaction. Perfect for projects of all sizes, **Laravel JSON** aims to streamline your development workflow, making JSON data handling a breeze.

Offered by [iteks](https://github.com/iteks/laravel-json), Developed by [jeramyhing](https://github.com/jeramyhing).

Get Started
-----------

[](#get-started)

> **Requires [PHP 8.1+](https://php.net/releases/)**

Install Laravel JSON via the [Composer](https://getcomposer.org/) package manager:

```
composer require iteks/laravel-json
```

Usage
-----

[](#usage)

Include the Json facade.

```
use Iteks\Support\Facades\Json;
```

- [Sample JSON Dataset](#sample-json-dataset)
- [JSON Helpers](#json-helpers)
    - [Json::toCollection()](#jsontocollection)
    - [Json::toArray()](#jsontoarray)
- [JSON Trait (DefinesJsonColumns)](#json-trait-definesjsoncolumns)
    - [Json::enforceDefinition()](#enforcedefinition)

Sample Json Dataset
-------------------

[](#sample-json-dataset)

This JSON dataset is used in the [Json::toCollection()](#jsontocollection) and [Json::toArray()](#jsontoarray) method examples.

```
[
    {
        "border": "3px solid white",
        "coordinates": [
          51.70696,
          40.34103
        ],
        "password": "xXeagle-*******"
    },
    {
        "border": "1px dotted amber",
        "coordinates": [
          11.80583,
          108.05094
        ],
        "password": "xXmeerkat-******"
    },
    {
        "border": "4px dotted gray",
        "coordinates": [
          116.82882,
          74.57905
        ],
        "password": "xXcat-*******"
    }
]
```

[top](#usage)

JSON Helpers
------------

[](#json-helpers)

### Json::toCollection()

[](#jsontocollection)

The `toCollection` method converts a JSON file into a Laravel collection of collections. This is particularly useful when you need to manipulate JSON data with the convenience and power of Laravel's Collection methods.

#### Without Argument Usage

[](#without-argument-usage)

When you use toCollection without specifying an attribute, it will simply convert the entire JSON file into a collection where each element is itself a collection representing the JSON objects.

```
$collection = Json::toCollection(database_path('data/test.json'));
```

```
Illuminate\Support\Collection {#298 ▼
  #items: array:3 [▼
    0 =>
Illuminate\Support\Collection {#299 ▼
      #items: array:3 [▼
        "border" => "3px solid white"
        "coordinates" => array:2 [▼
          0 => 51.70696
          1 => 40.34103
        ]
        "password" => "xXeagle-*******"
      ]
      #escapeWhenCastingToString: false
    }
    1 =>
Illuminate\Support\Collection {#300 ▼
      #items: array:3 [▼
        "border" => "1px dotted amber"
        "coordinates" => array:2 [▼
          0 => 11.80583
          1 => 108.05094
        ]
        "password" => "xXmeerkat-******"
      ]
      #escapeWhenCastingToString: false
    }
    2 =>
Illuminate\Support\Collection {#301 ▼
      #items: array:3 [▼
        "border" => "4px dotted gray"
        "coordinates" => array:2 [▼
          0 => 116.82882
          1 => 74.57905
        ]
        "password" => "xXcat-*******"
      ]
      #escapeWhenCastingToString: false
    }
  ]
  #escapeWhenCastingToString: false
}
```

```
// Iterate over the collection
foreach ($collection as $item) {
    // Access properties like in any Laravel collection
    echo $item->get('border');
}
```

#### With Argument Usage

[](#with-argument-usage)

When you provide an attribute name as the second argument, toCollection will create a collection where each item is the value of the specified attribute from the JSON objects.

```
$collection = Json::toCollection(database_path('data/test.json'), 'border');
```

```
Illuminate\Support\Collection {#297 ▼
  #items: array:3 [▼
    0 => "3px solid white"
    1 => "1px dotted amber"
    2 => "4px dotted gray"
  ]
  #escapeWhenCastingToString: false
}
```

[top](#usage)

### Json::toArray()

[](#jsontoarray)

The `toArray` method converts a JSON file into a PHP array. This method is ideal for when you need a simple array representation of your JSON data for further processing or when Laravel's Collection methods are not necessary.

#### Without Argument Usage

[](#without-argument-usage-1)

Without specifying an attribute, toArray converts the entire JSON file into a nested array, with each element being an associative array representing the JSON objects.

```
$array = Json::toArray(database_path('data/test.json'));
```

```
array:3 [▼
  0 => array:3 [▼
    "border" => "3px solid white"
    "coordinates" => array:2 [▼
      0 => 51.70696
      1 => 40.34103
    ]
    "password" => "xXeagle-*******"
  ]
  1 => array:3 [▼
    "border" => "1px dotted amber"
    "coordinates" => array:2 [▼
      0 => 11.80583
      1 => 108.05094
    ]
    "password" => "xXmeerkat-******"
  ]
  2 => array:3 [▼
    "border" => "4px dotted gray"
    "coordinates" => array:2 [▼
      0 => 116.82882
      1 => 74.57905
    ]
    "password" => "xXcat-*******"
  ]
]
```

```
// Access the array directly
foreach ($array as $item) {
    echo $item['border'];
}
```

#### With Argument Usage

[](#with-argument-usage-1)

Providing an attribute name as the second argument, toArray will generate an array containing only the values of the specified attribute from each JSON object.

```
$array = Json::toArray(database_path('data/test.json'), 'border');
```

```
array:3 [▼
  0 => "3px solid white"
  1 => "1px dotted amber"
  2 => "4px dotted gray"
]
```

[top](#usage)

JSON Trait (DefinesJsonColumns)
-------------------------------

[](#json-trait-definesjsoncolumns)

The `DefinesJsonColumns` trait and complementary `enforceDefinition` method allow you to define and enforce a JSON data structure for your JSON database columns. This ensures that any interaction with a JSON data column will consistently contain data that is structured according to its JSON definitions.

Simply apply the `DefinesJsonColumns` trait to your model that has JSON column(s) to define. Define the JSON structure with the `$jsonDefinitions` property on the model and begin using the `enforceDefinition` to enforce the column definition on your JSON input.

Import the model trait and configure your JSON definitions.

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Iteks\Support\Traits\DefinesJsonColumns;

class ExampleModel extends Model
{
  use DefinesJsonColumns;

  protected $jsonDefinitions = [
    'profile' => [
      'name' => 'string',
      'age' => 'integer',
      'avatar' => 'string',
    ],
    'address' => [
      'street' => 'string',
      'city' => 'string',
      'state' => 'string',
      'zip' => 'integer',
      'geo_coordinates' => 'array',
    ],
  ];
}
```

> You can configure definitions for multiple JSON columns.

### Json::enforceDefinition()

[](#jsonenforcedefinition)

You can use the `enforceDefinition` method anywhere in your application logic with target JSON input that you intend to insert into your model's JSON column.

```
$addressJson = json_encode($addressData);
$enforcedJson = Json::enforceDefinition(ExampleModel::class, 'address', $addressJson);

$exampleModel->address = $enforcedJson;
$exampleModel->save();
```

> If the input JSON is `null` or missing any defined keys, the definition will still be enforced by adding the missing keys with `null` values. If the input JSON contains additional key value pairs that are not in the JSON column definition, they will be excluded.

#### Usage in a form request's `prepareForValidation` method

[](#usage-in-a-form-requests-prepareforvalidation-method)

Apply the `enforceDefinition` method on the target request attribute that contains the JSON input. Pass the model class, column, and request attribute's JSON value.

```
namespace App\Http\Requests;

use App\Models\ExampleModel;
use Illuminate\Foundation\Http\FormRequest;
use Iteks\Support\Facades\Json;

class ExampleFormRequest extends FormRequest
{
    protected function prepareForValidation(): self
    {
        $this->merge([
            'request_attribute' => Json::enforceDefinition(ExampleModel::class, 'profile', $this->request_attribute),
        ]);

        return $this;
    }
}
```

[top](#usage)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance66

Regular maintenance activity

Popularity30

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

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 ~192 days

Total

5

Last Release

54d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/88e5ff66bb85d5340eea2ace11b581931c488785420335d62fc3b00235c29256?d=identicon)[jeramyhing](/maintainers/jeramyhing)

---

Top Contributors

[![jeramyhing](https://avatars.githubusercontent.com/u/107206200?v=4)](https://github.com/jeramyhing "jeramyhing (12 commits)")

---

Tags

phpjsonlaravelarraydataserializecollection

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/iteks-laravel-json/health.svg)

```
[![Health](https://phpackages.com/badges/iteks-laravel-json/health.svg)](https://phpackages.com/packages/iteks-laravel-json)
```

###  Alternatives

[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)[jbzoo/data

An extended version of the ArrayObject object for working with system settings or just for working with data arrays

891.6M23](/packages/jbzoo-data)[armincms/json

A Laravel Nova field.

25149.4k3](/packages/armincms-json)[hi-folks/data-block

Data class for managing nested arrays and JSON data.

1472.2k](/packages/hi-folks-data-block)[jshannon63/jsoncollect

Supercharge your JSON using collections

154.9k1](/packages/jshannon63-jsoncollect)[graze/data-structure

Data collections and containers

12287.4k8](/packages/graze-data-structure)

PHPackages © 2026

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