PHPackages                             laragear/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. [HTTP &amp; Networking](/categories/http)
4. /
5. laragear/json

ActiveLibrary[HTTP &amp; Networking](/categories/http)

laragear/json
=============

Easily retrieve and manipulate `Json` across your application.

v4.0.0(3mo ago)364.2k↓19.3%1MITPHPPHP ^8.3CI passing

Since Jul 5Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Laragear/Json)[ Packagist](https://packagist.org/packages/laragear/json)[ Fund](https://github.com/sponsors/DarkGhostHunter)[ Fund](https://paypal.me/darkghosthunter)[ RSS](/packages/laragear-json/feed)WikiDiscussions 4.x Synced 3d ago

READMEChangelog (6)Dependencies (6)Versions (11)Used By (0)

JSON Transport
==============

[](#json-transport)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f499607c7058cc1e907e6811080e26c4ed74abb91ffff909045355ef655c2b04/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c617261676561722f6a736f6e2e737667)](https://packagist.org/packages/laragear/json)[![Latest stable test run](https://github.com/Laragear/Json/workflows/Tests/badge.svg)](https://github.com/Laragear/MailLogin/actions)[![Codecov coverage](https://camo.githubusercontent.com/349e61c48eebb175748129eb1ac7d5f15c09b833b75b8da47c7a67081441c20a/68747470733a2f2f636f6465636f762e696f2f67682f4c617261676561722f4a736f6e2f67726170682f62616467652e7376673f746f6b656e3d327a45366c4b5779764f)](https://codecov.io/gh/Laragear/Json)[![Maintainability](https://camo.githubusercontent.com/dfcd50ce886ab2d850fe469de59d155ef2ee4e5a7f52ce4e814077911e200c64/68747470733a2f2f716c74792e73682f6261646765732f61323039346165632d313430622d346235352d626464312d6330663134313839646266372f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/Laragear/projects/Json)[![Sonarcloud Status](https://camo.githubusercontent.com/e313d3dfcf5dd81afbbf20bb6461a1528ee8a195f0aece676c26185aab12d5d0/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d4c617261676561725f4a736f6e266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=Laragear_Json)[![Laravel Octane Compatibility](https://camo.githubusercontent.com/70359a356da237cd29561bc5d0bb80baae775b5ff62f288ed324755382858342/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2532304f6374616e652d436f6d70617469626c652d737563636573733f7374796c653d666c6174266c6f676f3d6c61726176656c)](https://laravel.com/docs/10.x/octane#introduction)

Easily retrieve and manipulate `Json` across your application.

```
use Laragear\Json\Json;

$json = Json::fromJson('{"foo":"bar"}');

$json->set('bar.quz', 'quz');

echo $json->foo; // "quz"
```

Become a sponsor
----------------

[](#become-a-sponsor)

[![](.github/assets/support.png)](https://github.com/sponsors/DarkGhostHunter)

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **[spread the word!](http://twitter.com/share?text=I%20am%20using%20this%20cool%20PHP%20package&url=https://github.com%2FLaragear%2FJson&hashtags=PHP,Laravel)**.

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

[](#requirements)

- PHP 8.3 or later
- Laravel 12 or later

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

[](#installation)

Fire up Composer and require this package in your project.

```
composer require laragear/json

```

That's it.

Why is this for?
----------------

[](#why-is-this-for)

If you feel cumbersome to build complex JSON responses, or to deal with JSON trees values back and forth, this package is for you. It comes with a lot of features to make not only building and manipulating JSON trees.

```
// Before
$name = $json['users'][1]['name'] ?? null;

// After
$name = $json->get('users.1.name');
```

From the HTTP Request
---------------------

[](#from-the-http-request)

Simply use `getJson()` to automatically retrieve all the JSON from the request, or a given key value.

```
use Illuminate\Http\Request;
use Laragear\Json\Json;

public function data(Request $request)
{
    // Get the request JSON.
    $json = $request->getJson();

    // Return a key value from the JSON.
    $value = $json->get('this.is');

    // Set a value and return it.
    return $json->set('this.is', 'awesome');
}
```

Tip

You can still use the `json()` method to retrieve the JSON data as a `ParameterBag` or a key value.

As an HTTP Response
-------------------

[](#as-an-http-response)

You can build a `Json` instance using `make()`, optionally with your own `array`, but you can also use anything that implements the `Arrayable` contract or is *iterable*. Since the `Json` instance implements the `Responsable` trait, you can return it as-is and will be automatically transformed into a [JSON response](https://laravel.com/docs/10.x/responses#json-responses).

```
use Laragear\Json\Json;
use Illuminate\Support\Facades\Route;

Route::get('send', fn() => Json::make(['this_is' => 'cool']));
```

You can also transform it into a response and modify the outgoing response parameters, like the header or the status.

```
use Laragear\Json\Json;
use Illuminate\Support\Facades\Route;

Route::get('send', function() {
    return Json::make(['this_is' => 'cool'])
        ->toResponse()
        ->header('Content-Version', '1.0');
});
```

Creating an instance
--------------------

[](#creating-an-instance)

If you already have the `array` you want to transform into JSON, use the `make()` method or just instantiate it manually. Either way is fine. You can also use `Arrayable` objects and anything that is `iterable`, like [Collections](https://laravel.com/docs/10.x/collections).

```
use Laragear\Json\Json;

$json = new Json([
    'users' => [
        'id' => 1,
        'name' => 'John',
    ]
]);
```

If the value is already a JSON string, use `fromJson()`.

```
use Laragear\Json\Json;

$json = Json::fromJson('{"users":{"id":1,"name":"John"}}');
```

Available methods
-----------------

[](#available-methods)

The `Json` instance contains multiple helpful methods to make dealing with JSON data a breeze:

MethodDescription`get($key, $default = null)`Retrieves a key value in "dot" notation, returning a default value if its not set.`getMany([$key => $value], $default = null)`Retrieves an array of values keyed "dot" notation, filling a default value on those not set.`set($key, $value, $overwrite = true)`Sets a key in "dot" notation with a value.`setMany([$key => $value], $overwrite = true)`Sets multiple keys in "dot" notation with their respective values.`fill($key, $value)`Sets a key in "dot" notation with a value if the key doesn't exists or is `null`.`fillMany([$key => $value])`Sets multiple keys in "dot" notation with their respective values if they don't exist.`has($key)`Check if a key in "dot" notation is defined in the JSON.`hasAny(...$keys)`Check if at least one of the keys in "dot" notation is defined in the JSON.`missing($key)`Check if a key in "dot" notation is not defined in the JSON.`forget($key)`Removes (forgets) a key in "dot" notation from the JSON.`unset($key)`Removes (forgets) a key in "dot" notation from the JSON.`isSet($key)`Check if a key in "dot" notation exists and is not `null`.`isNotSet($key)`Check if a key in "dot" notation doesn't exists or is not `null`.`isEmpty()`Check if the JSON data is empty.`isNotEmpty()`Check if the JSON data is not empty.`keys()`Returns an array of all the root-level keys of the JSON.`only(...$keys)`Returns an array of only the issued root-level keys of the JSON.`except(...$keys)`Returns an array of all the root-level keys of the JSON except those issued.`segments($keys, $default = null)`Returns a `Json` instance a segment of the JSON data using keys in "dot" notation.`collect($key = null)`Returns a `Collection` instance from the JSON data, or value of a key in "dot" notation.`make($value = [])`Creates a new `Json` instance from an `iterable` or `Arrayable`value.`wrap($value)`Returns a `Json` instance from an `iterable` or `Arrayable` value if it's not `Json`.`fromJson($string)`Returns a `Json` instance from a JSON-encoded string.Eloquent JSON Cast
------------------

[](#eloquent-json-cast)

When dealing with JSON attributes in models, you will note that is really cumbersome to work with. Instead of using arrays or Collections, you can use the `AsJson` and `AsEncryptedJson` casts, that will offer a `Json` instance from the model property as-is or encrypted into the database, respectively.

Just add one of these to [your model casts](https://laravel.com/docs/10.x/eloquent-mutators#custom-casts).

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laragear\Json\Casts\AsJson;
use Laragear\Json\Casts\AsEncryptedJson;

class User extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'options' => AsJson::class,
        'secure_options' => AsEncryptedJson::class,
    ];
}
```

Once done, you can easily fill JSON into your model like normal.

```
use App\Models\User;
use Laragear\Json\Json;

$user = User::find();

// Set a Json instance, like from a string.
$user->options = Json::fromJson('{"apples":"tasty"}')

// Or just directly use an array tree.
$user->secure_options = [
    'visa' => [
        ['last_4' => '1234', 'preferred' => true]
    ]
];

// You can use the Json instance like a normal monday.
$user->secure_options->get('visa.last_4'); // "1234"
```

PhpStorm stubs
--------------

[](#phpstorm-stubs)

For users of PhpStorm, there is a stub file to aid in macro autocompletion for this package. You can publish them using the `phpstorm` tag:

```
php artisan vendor:publish --provider="Laragear\Json\JsonServiceProvider" --tag="phpstorm"
```

The file gets published into the `.stubs` folder of your project. You should point your [PhpStorm to these stubs](https://www.jetbrains.com/help/phpstorm/php.html#advanced-settings-area).

Laravel Octane compatibility
----------------------------

[](#laravel-octane-compatibility)

- There are no singletons using a stale application instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
- There are no static properties written.

There should be no problems using this package with Laravel Octane.

Security
--------

[](#security)

If you discover any security-related issues, please email  instead of using the issue tracker.

License
=======

[](#license)

This specific package version is licensed under the terms of the [MIT License](LICENSE.md), at the time of publishing.

[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011–2026 Laravel LLC.

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance78

Regular maintenance activity

Popularity32

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 97.3% 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 ~149 days

Recently: every ~101 days

Total

10

Last Release

119d ago

Major Versions

1.x-dev → v2.0.02024-03-06

v2.0.1 → v3.0.02025-03-28

3.x-dev → 4.x-dev2026-03-08

PHP version history (5 changes)v1.0.0PHP &gt;=8.0

v1.1.0PHP 8.\*

v2.0.0PHP ^8.1

v3.0.0PHP ^8.2

4.x-devPHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5141911?v=4)[Italo](/maintainers/DarkGhostHunter)[@DarkGhostHunter](https://github.com/DarkGhostHunter)

---

Top Contributors

[![DarkGhostHunter](https://avatars.githubusercontent.com/u/5141911?v=4)](https://github.com/DarkGhostHunter "DarkGhostHunter (36 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

httpjsonlaravel

### Embed Badge

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

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M151](/packages/laravel-mcp)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.9k3](/packages/defstudio-telegraph)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.0k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

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