
This is the same as the name in the name-value pair in JSON object eg $data->firstName. To access the PHP object data, you use the object operator ( ->) after the object name, followed by the key of the key-value pair. We use the built-in function json_decode() to convert the JSON string to the appropriate data type such as an object or an array. 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. Similar to POST or GET request, we use file_get_contents() but instead of having “php://input”, we use the file path.įor example, if we have a JSON file with path " ", we can access its data as below:
$mydata = file_get_contents("php://input") Ī JSON file contains a JSON object and has a file extension of. 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:
To receive JSON data as a POST request, we use the “php://input” along with the function file_get_contents() as below: In this post, we will cover how to decode a JSON object and access its data in PHP.īelow is an example of a simple JSON object: 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. It is easy to generate and parse in many programming languages.

Java Script Object Notation( JSON) is a lightweight human-readable text format for storing and transporting data consisting of name-value pairs and arrays. John Mwaniki / How to extract and access JSON data in PHP What is JSON?
