# JsonResponseFormatter

> JsonResponseFormatter formats the given data into a JSON or JSONP response content.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › JsonResponseFormatter

| class | `yii\web\JsonResponseFormatter` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`ResponseFormatterInterface`](/docs/api/yii/web/responseformatterinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/JsonResponseFormatter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/JsonResponseFormatter.php#L40) |

JsonResponseFormatter formats the given data into a JSON or JSONP response content.

It is used by [`Response`](/docs/api/yii/web/response/index.md) to format response data.

To configure properties like [`$encodeOptions`](/docs/api/yii/web/jsonresponseformatter/index.md#property-encodeOptions) or [`$prettyPrint`](/docs/api/yii/web/jsonresponseformatter/index.md#property-prettyPrint), you can configure the `response`
application component like the following:

```
'response' => [
    // ...
    'formatters' => [
        \yii\web\Response::FORMAT_JSON => [
             'class' => 'yii\web\JsonResponseFormatter',
             'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode
             'keepObjectType' => false, // keep object type for zero-indexed objects
             // ...
        ],
    ],
],
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$contentType`](#property-contentType) | `string\|null` | Custom value of the `Content-Type` header of the response. Default: `null`. Available since 2.0.14. | `JsonResponseFormatter` |
| [`$encodeOptions`](#property-encodeOptions) | `int` | The encoding options passed to [`Json::encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode). Default: `320`. Available since 2.0.7. | `JsonResponseFormatter` |
| [`$keepObjectType`](#property-keepObjectType) | `bool` | Avoids objects with zero-indexed keys to be encoded as array Json::encode((object)['test']) will be encoded as an object not array. Default: `null`. Available since 2.0.44. | `JsonResponseFormatter` |
| [`$prettyPrint`](#property-prettyPrint) | `bool` | Whether to format the output in a readable "pretty" format. Default: `false`. Available since 2.0.7. | `JsonResponseFormatter` |
| [`$useJsonp`](#property-useJsonp) | `bool` | Whether to use JSONP response format. Default: `false`. | `JsonResponseFormatter` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`format()`](#method-format) | Formats the specified response. | `JsonResponseFormatter` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`formatJson()`](#method-formatJson) protected | Formats response data in JSON format. | `JsonResponseFormatter` |
| [`formatJsonp()`](#method-formatJsonp) protected | Formats response data in JSONP format. | `JsonResponseFormatter` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-CONTENT_TYPE_JSONP"></span>`CONTENT_TYPE_JSONP` | `'application/javascript; charset=UTF-8'` | JSON Content Type Available since 2.0.14. | `JsonResponseFormatter` |
| <span id="const-CONTENT_TYPE_JSON"></span>`CONTENT_TYPE_JSON` | `'application/json; charset=UTF-8'` | JSONP Content Type Available since 2.0.14. | `JsonResponseFormatter` |
| <span id="const-CONTENT_TYPE_HAL_JSON"></span>`CONTENT_TYPE_HAL_JSON` | `'application/hal+json; charset=UTF-8'` | HAL JSON Content Type Available since 2.0.14. | `JsonResponseFormatter` |

## Property details

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

```php
public string|null $contentType = null
```

Custom value of the `Content-Type` header of the response.

When equals `null` default content type will be used based on the `useJsonp` property.

**Since**

2.0.14

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

```php
public int $encodeOptions = 320
```

The encoding options passed to [`Json::encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode).

For more details please refer to
<https://www.php.net/manual/en/function.json-encode.php>.
Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
This property has no effect, when [`$useJsonp`](/docs/api/yii/web/jsonresponseformatter/index.md#property-useJsonp) is `true`.

**Since**

2.0.7

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

```php
public bool $keepObjectType = null
```

Avoids objects with zero-indexed keys to be encoded as array Json::encode((object)['test']) will be encoded as an object not array.

This matches the behaviour of json_encode().
Defaults to Json::$keepObjectType value

**Since**

2.0.44

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

```php
public bool $prettyPrint = false
```

Whether to format the output in a readable "pretty" format.

This can be useful for debugging purpose.
If this is true, `JSON_PRETTY_PRINT` will be added to [`$encodeOptions`](/docs/api/yii/web/jsonresponseformatter/index.md#property-encodeOptions).
Defaults to `false`.
This property has no effect, when [`$useJsonp`](/docs/api/yii/web/jsonresponseformatter/index.md#property-useJsonp) is `true`.

**Since**

2.0.7

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

```php
public bool $useJsonp = false
```

Whether to use JSONP response format.

When this is true, the [response data](/docs/api/yii/web/response/index.md#property-data)
must be an array consisting of `data` and `callback` members. The latter should be a JavaScript
function name while the former will be passed to this function as a parameter.

## Method details

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

```php
public function format(yii\web\Response $response): mixed
```

Formats the specified response.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | [`Response`](/docs/api/yii/web/response/index.md) | The response to be formatted. |

**Returns**

`mixed`

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

```php
protected function formatJson(yii\web\Response $response): mixed
```

Formats response data in JSON format.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | [`Response`](/docs/api/yii/web/response/index.md) |  |

**Returns**

`mixed`

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

```php
protected function formatJsonp(yii\web\Response $response): mixed
```

Formats response data in JSONP format.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | [`Response`](/docs/api/yii/web/response/index.md) |  |

**Returns**

`mixed`