PHP has two built-in functions json_encode() and json_decode() to encode PHP objects into JSON string and decode JSON strings into PHP objects. To parse a JSON string, you can use the json_decode() function. This function recursively converts the passed JSON string into the corresponding PHP objects. You can control the parsing flow by passing a set of bitmasks to the JSON decoder function. If the JSON cannot be parsed, or the nested JSON data has a depth greater than the recursion limit, it returns NULL. The PHP JSON parser only works with UTF-8 encoded strings. In this PHP JSON Parse example, we use the json_decode() function to decode JSON strings into PHP objects. Click Execute to run the PHP Parse JSON Example online and see the result.
How to decode JSON data in PHP?
To decode JSON string in PHP, you can use the json_decode() function to convert the JSON string to the appropriate PHP data type. The example below shows how to parse or decode a JSON string to a PHP object:
To encode PHP objects into JSON strings, you can use the json_encode() function. A PHP object can be of any PHP data type, except for a resource such as a database or a file descriptor. The example below shows how to encode a PHP associative array into a JSON string: