# JsonParser

> Parses a raw HTTP request using yii\helpers\Json::decode().

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

| class | `yii\web\JsonParser` |
| --- | --- |
| Implements | [`RequestParserInterface`](/docs/api/yii/web/requestparserinterface/index.md) |
| Since | 2.0 |
| Source | [`framework/web/JsonParser.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/JsonParser.php#L30) |

Parses a raw HTTP request using [`Json::decode()`](/docs/api/yii/helpers/basejson/index.md#method-decode).

To enable parsing for JSON requests you can configure [`Request::$parsers`](/docs/api/yii/web/request/index.md#property-parsers) using this class:

```
'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-asArray"></span>`$asArray` | `bool` | Whether to return objects in terms of associative arrays. Default: `true`. | `JsonParser` |
| <span id="property-throwException"></span>`$throwException` | `bool` | Whether to throw a [`BadRequestHttpException`](/docs/api/yii/web/badrequesthttpexception/index.md) if the body is invalid JSON Default: `true`. | `JsonParser` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`parse()`](#method-parse) | Parses a HTTP request body. | `JsonParser` |

## Method details

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

```php
public function parse(string $rawBody, string $contentType): mixed
```

Parses a HTTP request body.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rawBody` | `string` | The raw HTTP request body. |
| `$contentType` | `string` | The content type specified for the request body. |

**Returns**

`mixed`: Parameters parsed from the request body

**Throws**

- [`BadRequestHttpException`](/docs/api/yii/web/badrequesthttpexception/index.md): if the body contains invalid json and [`$throwException`](/docs/api/yii/web/jsonparser/index.md#property-throwException) is `true`.