The Wayback Machine - https://web.archive.org/web/20240601144940/https://www.geeksforgeeks.org/how-to-json-decode-in-php/
Open In App

How to JSON Decode in PHP?

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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:

Using json_decode() method

In 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: GFG
Age: 30
City: Noida

Using Explode and Foreach Loop

In 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: GFG
Age: 30
City: Noida

Previous Article
Next Article

Similar Reads

How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more. Approach: We are going to fetch JSON data from one of free website,
2 min read
Which functions are used to encode and decode JSON file in PHP ?
JSON stands for JavaScript Object Notation. Like XML, it is a text-based format for the exchange of data which is easier to read and write and it is lighter than other formats. JSON is based on two basic structures namely Objects and Arrays. Parsing JSON data in PHP: There are built-in functions in PHP for both encoding and decoding JSON data. Thes
2 min read
How to Decode JSON Objects in Laravel & Apply for each Loop?
Decoding JSON objects in Laravel and applying for each loop is a fundamental aspect of working with data in Laravel applications. When you receive JSON data in your Laravel application, you need to decode it to work with it effectively. Applying for each loop allows you to iterate through the decoded JSON objects or arrays to manipulate the data as
2 min read
How to Encode and Decode URLs in PHP ?
In PHP, encoding and decoding URLs is a common task when working with web applications, especially when handling user input or constructing URLs dynamically. URL encoding is necessary to ensure that special characters in URLs are properly represented, while URL decoding is used to retrieve the original data from encoded URLs. Syntax// Encode a URL$
2 min read
How to Encode and Decode a URL in PHP?
Encoding and decoding URLs in PHP are essential tasks when working with web applications, especially for handling user input and constructing query strings. URL encoding ensures that special characters are properly represented in URLs, preventing errors or unexpected behavior during data transmission. PHP provides built-in functions to encode and d
2 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
Difference between package.json and package-lock.json files
In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node. In Node, package.json is a versioning file used to install multiple packages in your project. As you initialize your node application, you will see three files installed in your app that is node_modules, package.json, and package
3 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
Article Tags :