It's already decoded, as you can see on the man pages, the default behavior of Show
But you can just access the John Mwaniki / 04 Sep 2021 What is JSON?JavaScript Object Notation(JSON) is a lightweight human-readable text format for storing and transporting data consisting of name-value pairs and arrays. It is easy to generate and parse in many programming languages. It is the most popular and lightweight data-interchange format for web applications, and the de-facto format for the data exchange in RESTful web services requests and responses. In this post, we will cover how to decode a JSON object and access its data in PHP. Below is an example of a simple JSON object:
How to receive JSON data in PHP1. From a POST or GET requestTo receive JSON data as a POST request, we use the “php://input” along with the function file_get_contents() as below:
For instance, the JSON data is sent below as a POST request:
To receive the above request data in the register.php file, just add file_get_contents("php://input") and assign it to a variable for processing eg:
2. Reading a JSON fileA JSON file contains a JSON object and has a file extension of .json. You can as well open the file in PHP and access its data. Similar to POST or GET request, we use file_get_contents() but instead of having “php://input”, we use the file path. For example, if we have a JSON file with path "https://www.example.com/mydata.json", we can access its data as below:
If the json file and the PHP file accessing it are in the same website, we can use relative path instead of the full file URL. Extracting/Decoding JSON data in PHPWe use the built-in function Syntax:
1. Accessing JSON data as a PHP objectBy default the json_decode() function returns an object. Example
The above example outputs below: object(stdClass)#1 (4) { ["firstName"]=> string(4) "John" ["lastName"]=> string(3) "Doe" ["email"]=> string(17) "" ["phone"]=> string(12) "111-111-1111" } To access the PHP object data, you use the object operator (->) after the object name, followed by the key of the key-value pair. This is the same as the name in the name-value pair in JSON object eg $data->firstName . Example
2. Accessing JSON data as an arrayYou can as well convert the JSON object to a PHP associative array by passing a second(optional) parameter in the json_decode() function with the boolean value "true" as below. The value is set to false by default if you don't pass it.
The example below decodes JSON object into a PHP associative array:
The above example outputs below: array(4) { ["firstName"]=> string(4) "John" ["lastName"]=> string(3) "Doe" ["email"]=> string(17) "" ["phone"]=> string(12) "111-111-1111" } You access the data as in any other PHP associative array as in the example below:
3. Accessing data in a nested JSON objectA JSON object may comprise of json objects and arrays as the values in its name-value pairs such as in the example below:
In the above example, the "address" has an object as its value while "siblings" has an array value comprising of objects. The easiest way of accessing all the data is decoding the object as an associative array. Example
Looping through an object of objects with foreach()You may have a large JSON object made of an array of objects, like in the example below:
To access the values of a country in the example above, you just have to know its object position in the array. For example, china is in the third position. But when accessing the array items, we start counting from 0, hence the index of China in the array is 2. We access the China array object as below:
If you want to access all the array data then it can be tiresome and time-consuming to write the code for accessing each at a time especially when the object is large. For such an instance, you can use the foreach() function to loop through all the objects as below:
ConclusionIn this post, we have covered everything you need to know in extracting and accessing a JSON object data using PHP. You may also want to learn how to form a JSON object in PHP. If you would want to get notified via email when we add more incredible content in our blog, kindly subscribe to our email newsletter. How to get a value from JSON in PHP?To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
How can access JSON decoded data in PHP?PHP and JSON. The json_encode() function is used to encode a value to JSON format.. The json_decode() function is used to decode a JSON object into a PHP object or an associative array.. The json_decode() function returns an object by default. ... . You can also loop through the values with a foreach() loop:. How are JSON values accessed in PHP with example?PHP File explained:
Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.
|