# JsonExpression

> Class JsonExpression represents data that should be encoded to JSON.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › JsonExpression

| class | `yii\db\JsonExpression` |
| --- | --- |
| Implements | [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md), [`JsonSerializable`](https://www.php.net/manual/en/class.jsonserializable.php) |
| Since | 2.0.14 |
| Source | [`framework/db/JsonExpression.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/JsonExpression.php#L25) |

Class JsonExpression represents data that should be encoded to JSON.

For example:

```
new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$type`](#property-type) protected | `string\|null` | Type of JSON, expression should be casted to. Default: `null`. | `JsonExpression` |
| [`$value`](#property-value) protected | `mixed` | The value to be encoded to JSON. Default: `null`. | `JsonExpression` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | JsonExpression constructor. | `JsonExpression` |
| [`getType()`](#method-getType) |  | `JsonExpression` |
| [`getValue()`](#method-getValue) |  | `JsonExpression` |
| [`jsonSerialize()`](#method-jsonSerialize) | Specify data which should be serialized to JSON | `JsonExpression` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-TYPE_JSON"></span>`TYPE_JSON` | `'json'` |  | `JsonExpression` |
| <span id="const-TYPE_JSONB"></span>`TYPE_JSONB` | `'jsonb'` |  | `JsonExpression` |

## Property details

### $type <span id="property-type"></span>

```php
protected string|null $type = null
```

Type of JSON, expression should be casted to.

Defaults to `null`, meaning
no explicit casting will be performed.
This property will be encountered only for DBMSs that support different types of JSON.
For example, PostgreSQL has `json` and `jsonb` types.

### $value <span id="property-value"></span>

```php
protected mixed $value = null
```

The value to be encoded to JSON.

The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] input requirements.

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(mixed $value, string|null $type = null)
```

JsonExpression constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The value to be encoded to JSON. The value must be compatible with [\yii\helpers\Json::encode()\|Json::encode()]] requirements. |
| `$type` | `string\|null` | The type of the JSON. See [`JsonExpression::$type`](/docs/api/yii/db/jsonexpression/index.md#property-type) |

**See also**

- [`$type`](/docs/api/yii/db/jsonexpression/index.md#property-type)

### getType() <span id="method-getType"></span>

```php
public function getType(): string|null
```

**Returns**

`string|null`: The type of JSON

**See also**

- [`$type`](/docs/api/yii/db/jsonexpression/index.md#property-type)

### getValue() <span id="method-getValue"></span>

```php
public function getValue(): mixed
```

**Returns**

`mixed`

**See also**

- [`$value`](/docs/api/yii/db/jsonexpression/index.md#property-value)

### jsonSerialize() <span id="method-jsonSerialize"></span>

```php
public function jsonSerialize(): mixed
```

Specify data which should be serialized to JSON

**Returns**

`mixed`: Data which can be serialized by &lt;b>json_encode&lt;/b>, which is a value of any type other than a resource.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when JsonExpression contains QueryInterface object

**Since**

2.0.14.2