PHP - JSON
The JSON extension implements the JavaScript Object Notation (JSON) data-interchange format. PHP comes with a parser that is specifically written for PHP and licensed under the PHP license.
Installation
The JSON extension is bundled and compiled into PHP by default. As of PHP 8.0.0, the JSON extension is a core PHP extension, so it is always enabled.
Runtime Configuration
This extension has no configuration directives defined in php.ini.
PHP JsonException Class
Class Description | |
---|---|
JsonException | The JsonException class. |
PHP JsonSerializable Interface
Interface Description | |
---|---|
JsonSerializable | The JsonSerializable interface. |
Methods | Description |
jsonSerialize() | Specify data which should be serialized to JSON. |
PHP JSON Functions
Functions | Description |
---|---|
json_decode() | Decodes a JSON string. |
json_encode() | Returns the JSON representation of a value. |
json_last_error_msg() | Returns the error string of the last json_encode() or json_decode() call. |
json_last_error() | Returns the last error occurred. |
PHP JSON Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
The following constants indicate the type of error returned by json_last_error() or stored as the code of a JsonException.
Constants | Type | Description |
---|---|---|
JSON_ERROR_NONE | Integer | No error has occurred. |
JSON_ERROR_DEPTH | Integer | The maximum stack depth has been exceeded. |
JSON_ERROR_STATE_MISMATCH | Integer | Occurs with underflow or with the modes mismatch. |
JSON_ERROR_CTRL_CHAR | Integer | Control character error, possibly incorrectly encoded. |
JSON_ERROR_SYNTAX | Integer | Syntax error. |
JSON_ERROR_UTF8 | Integer | Malformed UTF-8 characters, possibly incorrectly encoded. |
JSON_ERROR_RECURSION | Integer | The object or array passed to json_encode() include recursive references and cannot be encoded. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, null will be encoded in the place of the recursive reference. |
JSON_ERROR_INF_OR_NAN | Integer | The value passed to json_encode() includes either NAN or INF. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, 0 will be encoded in the place of these special numbers. |
JSON_ERROR_UNSUPPORTED_TYPE | Integer | A value of an unsupported type was given to json_encode(), such as a resource. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, null will be encoded in the place of the unsupported value. |
JSON_ERROR_INVALID_PROPERTY_NAME | Integer | A key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object. |
JSON_ERROR_UTF16 | Integer | Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_decode(). |
The following constants can be combined to form options for json_decode().
Constants | Type | Description |
---|---|---|
JSON_BIGINT_AS_STRING | Integer | Decodes large integers as their original string value. |
JSON_OBJECT_AS_ARRAY | Integer | Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to true. |
The following constants can be combined to form options for json_encode().
Constants | Type | Description |
---|---|---|
JSON_HEX_TAG | Integer | All < and > are converted to \u003C and \u003E. |
JSON_HEX_AMP | Integer | All & are converted to \u0026. |
JSON_HEX_APOS | Integer | All ' are converted to \u0027. |
JSON_HEX_QUOT | Integer | All " are converted to \u0022. |
JSON_FORCE_OBJECT | Integer | Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. |
JSON_NUMERIC_CHECK | Integer | Encodes numeric strings as numbers. |
JSON_PRETTY_PRINT | Integer | Use whitespace in returned data to format it. |
JSON_UNESCAPED_SLASHES | Integer | Don't escape /. |
JSON_UNESCAPED_UNICODE | Integer | Encode multibyte Unicode characters literally (default is to escape as \uXXXX). |
JSON_PARTIAL_OUTPUT_ON_ERROR | Integer | Substitute some unencodable values instead of failing. |
JSON_PRESERVE_ZERO_FRACTION | Integer | Ensures that float values are always encoded as a float value. |
JSON_UNESCAPED_LINE_TERMINATORS | Integer | The line terminators are kept unescaped when JSON_UNESCAPED_UNICODE is supplied. It uses the same behavior as it was before PHP 7.1 without this constant. Available as of PHP 7.1.0. |
The following constants can be combined to form options for json_decode() and json_encode().
Constants | Type | Description |
---|---|---|
JSON_INVALID_UTF8_IGNORE | Integer | Ignore invalid UTF-8 characters. Available as of PHP 7.2.0. |
JSON_INVALID_UTF8_SUBSTITUTE | Integer | Convert invalid UTF-8 characters to \0xfffd (Unicode Character 'REPLACEMENT CHARACTER') Available as of PHP 7.2.0. |
JSON_THROW_ON_ERROR | Integer | Throws JsonException if an error occurs instead of setting the global error state that is retrieved with json_last_error() and json_last_error_msg(). JSON_PARTIAL_OUTPUT_ON_ERROR takes precedence over JSON_THROW_ON_ERROR. Available as of PHP 7.3.0. |