JSON Functions
**************
.[perex]
[api:Nette\Utils\Json] is a static class with JSON encoding and decoding functions. It handles vulnerabilities in different PHP versions and throws exceptions on errors.
Installation:
```shell
composer require nette/utils
```
All examples assume the following class alias is defined:
```php
use Nette\Utils\Json;
```
Usage
=====
encode(mixed $value, int $flags=0): string .[method]
----------------------------------------------------
Converts `$value` to JSON format. The flag can be `Json::PRETTY`, which formats JSON for easier reading and clarity, and `Json::ESCAPE_UNICODE` for ASCII output.
```php
Json::encode($value); // returns JSON
Json::encode($value, Json::PRETTY); // returns readable JSON
Json::encode($value, Json::PRETTY | Json::ESCAPE_UNICODE); // combination
```
It throws an `Nette\Utils\JsonException` exception on error.
```php
try {
$json = Json::encode($value);
} catch (Nette\Utils\JsonException $e) {
// Exception handling
}
```
decode(string $json, int $flags=0): mixed .[method]
---------------------------------------------------
Parses JSON to PHP value. The flag can be `Json::FORCE_ARRAY`, which forces an array instead of an object as the return value.
```php
Json::decode('{"variable": true}'); // returns an object of stdClass
Json::decode('{"variable": true}', Json::FORCE_ARRAY); // returns an array
Json::decode('true'); // return true
```
It throws an `Nette\Utils\JsonException` exception on error.
```php
try {
$value = Json::decode($json);
} catch (Nette\Utils\JsonException $e) {
// Exception handling
}
```
How to Send a JSON from a Presenter?
====================================
You can use the `$this->sendJson($data)` method, which can be called, for example, in the `action*()` method, see [Sending a Response|application:presenters#Sending a Response].
{{maintitle: PHP JSON Encode and Decode with Exceptions: Nette Utils}}
{{description: Encode and decode JSON in PHP with the Json class, which throws exceptions on errors. Plus how to send JSON from a Nette presenter.}}
JSON Functions
Nette\Utils\Json is a static class with
JSON encoding and decoding functions. It handles vulnerabilities in different PHP versions and throws exceptions on errors.
Installation:
composer require nette/utils
All examples assume the following class alias is defined:
use Nette\Utils\Json;
Usage
encode (mixed $value, int $flags=0): string
Converts $value to JSON format. The flag can be Json::PRETTY, which formats JSON for easier reading
and clarity, and Json::ESCAPE_UNICODE for ASCII output.
Json::encode($value); // returns JSON
Json::encode($value, Json::PRETTY); // returns readable JSON
Json::encode($value, Json::PRETTY | Json::ESCAPE_UNICODE); // combination
It throws an Nette\Utils\JsonException exception on error.
try {
$json = Json::encode($value);
} catch (Nette\Utils\JsonException $e) {
// Exception handling
}
decode (string $json, int $flags=0): mixed
Parses JSON to PHP value. The flag can be Json::FORCE_ARRAY, which forces an array instead of an object as the
return value.
Json::decode('{"variable": true}'); // returns an object of stdClass
Json::decode('{"variable": true}', Json::FORCE_ARRAY); // returns an array
Json::decode('true'); // return true
It throws an Nette\Utils\JsonException exception on error.
try {
$value = Json::decode($json);
} catch (Nette\Utils\JsonException $e) {
// Exception handling
}
How to Send a JSON from a Presenter?
You can use the $this->sendJson($data) method, which can be called, for example, in the action*()
method, see Sending a Response.