PHP Function Reference

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.
MethodsDescription
jsonSerialize() Specify data which should be serialized to JSON.

PHP JSON Functions

FunctionsDescription
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.

ConstantsTypeDescription
JSON_ERROR_NONEIntegerNo error has occurred.
JSON_ERROR_DEPTHIntegerThe maximum stack depth has been exceeded.
JSON_ERROR_STATE_MISMATCHIntegerOccurs with underflow or with the modes mismatch.
JSON_ERROR_CTRL_CHARIntegerControl character error, possibly incorrectly encoded.
JSON_ERROR_SYNTAXIntegerSyntax error.
JSON_ERROR_UTF8IntegerMalformed UTF-8 characters, possibly incorrectly encoded.
JSON_ERROR_RECURSIONIntegerThe 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_NANIntegerThe 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_TYPEIntegerA 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_NAMEIntegerA key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object.
JSON_ERROR_UTF16IntegerSingle 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().

ConstantsTypeDescription
JSON_BIGINT_AS_STRINGIntegerDecodes large integers as their original string value.
JSON_OBJECT_AS_ARRAYIntegerDecodes 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().

ConstantsTypeDescription
JSON_HEX_TAGIntegerAll < and > are converted to \u003C and \u003E.
JSON_HEX_AMPIntegerAll & are converted to \u0026.
JSON_HEX_APOSIntegerAll ' are converted to \u0027.
JSON_HEX_QUOTIntegerAll " are converted to \u0022.
JSON_FORCE_OBJECTIntegerOutputs 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_CHECKIntegerEncodes numeric strings as numbers.
JSON_PRETTY_PRINTIntegerUse whitespace in returned data to format it.
JSON_UNESCAPED_SLASHESIntegerDon't escape /.
JSON_UNESCAPED_UNICODEIntegerEncode multibyte Unicode characters literally (default is to escape as \uXXXX).
JSON_PARTIAL_OUTPUT_ON_ERRORIntegerSubstitute some unencodable values instead of failing.
JSON_PRESERVE_ZERO_FRACTIONIntegerEnsures that float values are always encoded as a float value.
JSON_UNESCAPED_LINE_TERMINATORSIntegerThe 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().

ConstantsTypeDescription
JSON_INVALID_UTF8_IGNOREIntegerIgnore invalid UTF-8 characters. Available as of PHP 7.2.0.
JSON_INVALID_UTF8_SUBSTITUTEIntegerConvert invalid UTF-8 characters to \0xfffd (Unicode Character 'REPLACEMENT CHARACTER') Available as of PHP 7.2.0.
JSON_THROW_ON_ERRORIntegerThrows 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.