# Json

> Json is a helper class providing JSON data encoding and decoding.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [helpers](/docs/api/yii/helpers/index.md) › Json

| class | `yii\helpers\Json` |
| --- | --- |
| Extends | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| Since | 2.0 |
| Source | [`framework/helpers/Json.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/helpers/Json.php#L18) |

Json is a helper class providing JSON data encoding and decoding.

It enhances the PHP built-in functions `json_encode()` and `json_decode()`
by supporting encoding JavaScript expressions and throwing exceptions when decoding fails.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-jsonErrorMessages"></span>[`$jsonErrorMessages`](/docs/api/yii/helpers/basejson/index.md#property-jsonErrorMessages) static | `array` | List of JSON Error messages assigned to constant names for better handling of PHP &lt;= 5.5. Default: `['JSON_ERROR_SYNTAX' => 'Syntax error', 'JSON_ERROR_UNSUPPORTED_TYPE' => 'Type is not supported', 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded', 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON', 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded', 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded']`. Available since 2.0.7. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="property-keepObjectType"></span>[`$keepObjectType`](/docs/api/yii/helpers/basejson/index.md#property-keepObjectType) static | `bool` | Avoids objects with zero-indexed keys to be encoded as array `Json::encode((object)['test'])` will be encoded as an object not as an array. Default: `false`. Available since 2.0.44. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="property-prettyPrint"></span>[`$prettyPrint`](/docs/api/yii/helpers/basejson/index.md#property-prettyPrint) static | `bool\|null` | Enables human readable output a.k.a. Default: `null`. Available since 2.0.43. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-decode"></span>[`decode()`](/docs/api/yii/helpers/basejson/index.md#method-decode) static | Decodes the given JSON string into a PHP data structure. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="method-encode"></span>[`encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode) static | Encodes the given value into a JSON string. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="method-errorSummary"></span>[`errorSummary()`](/docs/api/yii/helpers/basejson/index.md#method-errorSummary) static | Generates a summary of the validation errors. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="method-htmlEncode"></span>[`htmlEncode()`](/docs/api/yii/helpers/basejson/index.md#method-htmlEncode) static | Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="method-handleJsonError"></span>[`handleJsonError()`](/docs/api/yii/helpers/basejson/index.md#method-handleJsonError) protected static | Handles [`encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode) and [`decode()`](/docs/api/yii/helpers/basejson/index.md#method-decode) errors by throwing exceptions with the respective error message. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |
| <span id="method-processData"></span>[`processData()`](/docs/api/yii/helpers/basejson/index.md#method-processData) protected static | Pre-processes the data before sending it to `json_encode()`. | [`BaseJson`](/docs/api/yii/helpers/basejson/index.md) |