Working with JSON

Nette\Utils\Json is a static class with functions for encoding and decoding the JSON format. It addresses vulnerabilities in various PHP versions and throws exceptions upon errors.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Json;

Usage

encode (mixed $value, bool $pretty=false, bool $asciiSafe=false, bool $htmlSafe=false, bool $forceObjects=false)string

Converts $value to JSON format.

Setting $pretty to true formats the JSON for better readability and clarity:

Json::encode($value); // returns JSON
Json::encode($value, pretty: true); // returns more readable JSON

With $asciiSafe set to true, it generates ASCII output, meaning Unicode characters are replaced with \uxxxx sequences:

Json::encode('žluťoučký', asciiSafe: true);
// '"\u017elu\u0165ou\u010dk\u00fd"'

The $htmlSafe parameter ensures that the output does not contain characters with special meaning in HTML:

Json::encode('one<two & three', htmlSafe: true);
// '"one\u003Ctwo \u0026 three"'

With $forceObjects set to true, even arrays with numeric keys will be encoded as JavaScript objects:

Json::encode(['a', 'b', 'c']);
// '["a","b","c"]'
Json::encode(['a', 'b', 'c'], forceObjects: true);
// '{"0":"a","1":"b","2":"c"}'

Throws a Nette\Utils\JsonException on error.

try {
	$json = Json::encode($value);
} catch (Nette\Utils\JsonException $e) {
	// Exception handling
}

decode (string $json, bool $forceArray=false)mixed

Parses a JSON string into a PHP value.

Setting $forceArray to true forces the return of arrays instead of objects:

Json::decode('{"variable": true}'); // returns an object of type stdClass
Json::decode('{"variable": true}', forceArray: true); // returns an array

Throws a Nette\Utils\JsonException on error.

try {
	$value = Json::decode($json);
} catch (Nette\Utils\JsonException $e) {
	// Exception handling
}

How to Send JSON from a Presenter?

You can use the $this->sendJson($data) method, which can be called, for example, within an action*() method. See Sending a Response.

version: 4.0 3.x 2.x