Php decode json from file
In this article, we are going to parse the JSON file by displaying JSON data using PHP. PHP is a server-side scripting language used to process the data. JSON stands for JavaScript object notation. JSON data is written as name/value pairs. Show Syntax:
Example: The JSON notation for student details is as follows.
Advantages:
Approach: Create a JSON file and save it as my_data.json. We have taken student data in the file. The contents are as follows.
Use file_get_contents() function to read JSON file into PHP. This function is used to read the file into PHP code. Syntax:
It is used to convert the JSON into an array. Syntax:
PHP code: The following is thePHP code to parse JSON file. PHP
Output: Array ( [Student] => Array ( [0] => Array ( [Name] => Sravan [Roll] => 7058 [subject] => java ) [1] => Array ( [Name] => Jyothika [Roll] => 7059 [subject] => SAP ) ) ) I have the following file: data.txt
I am quite new at PHP. Do you know how I can use the decode the above JSON using PHP? My PHP code
asked Jun 10, 2011 at 22:34
1 json_decode takes a string as an argument. Read in the
file with
You do need to adjust your sample string to be valid JSON by adding quotes around strings, commas between objects and placing the objects inside a containing array (or object).
answered Jun 10, 2011 at 22:36
brian_dbrian_d 11k4 gold badges46 silver badges72 bronze badges 1 As I mentioned in your other question you are not producing valid JSON. See my answer there, on how to create it. That will lead to something like
(I dont know, where your quotes are gone, but And this is easy readable
answered Jun 10, 2011 at 22:37
KingCrunchKingCrunch 126k20 gold badges147 silver badges171 bronze badges Your JSON data is invalid. You have multiple objects there (and you're missing quotes), you need some way to separate them before you feed the to answered Jun 10, 2011 at 22:36
HalcyonHalcyon 56.6k10 gold badges87 silver badges126 bronze badges
But your JSON needs to be formatted correctly:
answered Jun 10, 2011 at 22:36
MatthewMatthew 46.8k11 gold badges85 silver badges97 bronze badges That's not a valid JSON file, according to JSONLint. If it were, you'd have to read it first:
answered Jun 10, 2011 at 22:38
phihagphihag 268k67 gold badges439 silver badges462 bronze badges You have to read the file!
answered Jun 10, 2011 at 22:37
powtacpowtac 39.8k27 gold badges113 silver badges167 bronze badges How do I decode a JSON file?You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.
Can PHP read JSON file?Use file_get_contents() function to read JSON file into PHP. This function is used to read the file into PHP code.
How can I get JSON encoded data 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 - Accessing the Decoded Values
$obj = json_decode($jsonobj);
|