PHPackages                             laxity7/yii2-json-field - 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. laxity7/yii2-json-field

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

laxity7/yii2-json-field
=======================

Behavior that convert array to JSON before save data in model, and also convert JSON to an array after saving and retrieving data

v1.0.2(2y ago)484.7k↓12%1MITPHPPHP ^7.4||^8.0

Since Feb 21Pushed 2y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (4)Used By (0)

Yii2 JsonFieldBehavior
======================

[](#yii2-jsonfieldbehavior)

[![License](https://camo.githubusercontent.com/38b5d8ea80e5efad4341a07e0a87feffc660d924b032f9c3f085d2c72e5a670a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6178697479372f796969322d6a736f6e2d6669656c642e737667)](https://github.com/laxity7/yii2-json-field/blob/master/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/228886f0de2ca778dbab047d05cd1e4684c426bc627239ee04e4718f31431175/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6178697479372f796969322d6a736f6e2d6669656c642e737667)](https://packagist.org/packages/laxity7/yii2-json-field)[![Total Downloads](https://camo.githubusercontent.com/db85469595a9fdc32998d7dfbccc1b1fef22b131b10edf99b67b8b46b6284291/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6178697479372f796969322d6a736f6e2d6669656c642e737667)](https://packagist.org/packages/laxity7/yii2-json-field)

This behavior adds advanced support for working with JSON data in Yii2 active record models. Behavior convert array to JSON before save data in model, and also convert JSON to an array after saving and retrieving data. Use JSON fields like normal fields with an array or object.

Install
-------

[](#install)

Install via composer

```
composer require laxity7/yii2-json-field
```

How to use
----------

[](#how-to-use)

To use JsonFieldBehavior, insert the following code to your ActiveRecord class:

```
/** @inheritdoc */
public function behaviors(): array
{
   return [
       [
           'class'  => \laxity7\yii2\behaviors\JsonFieldBehavior::class,
           'fields' => ['foo_data', 'bar_data'],
       ],
   ];
}
```

You can also pass the following parameters:

- **jsonOptions** `int` (by default `JSON_UNESCAPED_UNICODE`) [JSON constants](http://php.net/manual/en/json.constants.php) can be combined to form options for json\_encode().
- **defaultValue** `array|string` (by default `'[]'`) The default value for attribute. This value by default will be stored in the database if the field value is empty. Ignored if \[\[skipEmpty\]\] is enabled.
- **skipEmpty** `bool` (by default `true`) Whether to skip a field if it's empty. When TRUE in the database, the field can be null, when FALSE will save an empty object ('\[\]' or see defaultValue)
- **asArray** `bool` (by default `true`) Decode JSON into an array or object.

So, the complete list of settings will look like this:

```
use laxity7\yii2\behaviors\JsonFieldBehavior;
use yii\db\ActiveRecord;

/**
* @property int $id
* @property array $foo_data
* @property array $bar_data
 */
class Foo extends ActiveRecord {
    /** @inheritdoc */
    public function behaviors(): array
    {
        return [
            [
                'class'        => JsonFieldBehavior::class,
                'fields'       => ['foo_data', 'bar_data'],
                'jsonOptions'  => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
                'skipEmpty'    => false,
                'defaultValue' => ['foo' => 'bar'],
                'asArray'      => true,
            ],
        ];
    }

    // Based on these parameters may be approximately the code
    public function updateBar(int $id, array $barData): array
    {
        $model = self::findOne(['id' => $id]);
        $model->foo_data['foo'] = 'bar1';
        $model->bar_data['bar'] = array_merge($model->bar_data['bar'], $barData);
        $model->save();

        return $model->bar_data['bar'];
    }
}
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity67

Established project with proven stability

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

Total

3

Last Release

991d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.4

v1.0.1PHP ^7.4||^8.0

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

phpyii2yii2-extensionjsonbehaviorsyii2

### Embed Badge

![Health badge](/badges/laxity7-yii2-json-field/health.svg)

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

###  Alternatives

[kdn/yii2-json-editor

JSON editor widget (josdejong/jsoneditor) for Yii 2.

22570.0k3](/packages/kdn-yii2-json-editor)[cranetm/yii2-json-rpc-2.0

JSON RPC 2.0 for Yii2 strict type validation of request and response data

2679.6k1](/packages/cranetm-yii2-json-rpc-20)[nizsheanez/yii2-json-rpc

A lightweight JsonRpc Server and Client for PHP

2034.0k](/packages/nizsheanez-yii2-json-rpc)[devgroup/yii2-jsoneditor

Yii2 jsoneditor widget

1199.6k1](/packages/devgroup-yii2-jsoneditor)

PHPackages © 2026

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