How to JSON Decode in PHP? Last Updated : 17 Apr, 2024 Comments Improve Suggest changes Like Article Like Report JSON Decode is the process of converting a JSON string into a PHP variable, typically an object or an associative array. Then we will print that decode JSON text. Below are the approaches to JSON decoding in PHP: Table of Content Using json_decode() methodUsing Explode and Foreach LoopUsing json_decode() methodIn this approach, we use json_decode() in PHP to parse a JSON string into an object. We then check if the decoding was successful and display the decoded values such as Name, Age, and City otherwise, it shows a failure message. Example: The below example uses json_decode() to JSON Decode in PHP. PHP <?php $json_data = '{"name": "GFG", "age": 30, "city": "Noida"}'; $res = json_decode($json_data); if ($res !== null) { echo "Name: " . $res->name . "<br>"; echo "Age: " . $res->age . "<br>"; echo "City: " . $res->city . "<br>"; } else { echo "Failed to decode JSON data."; } ?> Output: Name: GFGAge: 30City: NoidaUsing Explode and Foreach LoopIn this approach, This PHP code decodes JSON data using the explode function and a foreach loop. It replaces unnecessary characters, splits the data into key-value pairs, and stores them in an associative array. Finally, it prints specific values like name, age, and city. Example: The below example uses Explode and Foreach Loop to JSON Decode in PHP. PHP <?php $json_data = '{"name": "GFG", "age": 30, "city": "Noida"}'; $json_data = str_replace(["{", "}", '"'], "", $json_data); $key_Val = explode(",", $json_data); $res = []; foreach ($key_Val as $pair) { list($key, $value) = explode(":", $pair, 2); $key = trim($key); $value = trim($value); $res[$key] = $value; } echo "Name: " . $res["name"] . "<br>"; echo "Age: " . $res["age"] . "<br>"; echo "City: " . $res["city"] . "<br>"; ?> Output: Name: GFGAge: 30City: Noida Comment More infoAdvertise with us Next Article How to JSON Decode in PHP? A anjalibo6rb0 Follow Improve Article Tags : PHP JSON Similar Reads How to JSON decode in Python? When working with JSON data in Python, we often need to convert it into native Python objects. This process is known as decoding or deserializing. The json module in Python provides several functions to work with JSON data, and one of the essential tools is the json.JSONDecoder() method. This method 5 min read How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a 2 min read How to generate Json File in PHP ? In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_dat 3 min read PHP | json_decode() Function The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. Syntax: json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 ) Parameters: This function accepts four parameters as mentioned above and des 2 min read How to parse a JSON File in PHP? Parsing JSON in PHP is simple and efficient. JSON (JavaScript Object Notation) is a lightweight format used to store data in key-value pairs. PHP offers built-in functions to handle JSON encoding and decoding, making it easy to work with JSON data.JSON Syntax OverviewThe general syntax of a JSON obj 3 min read Like