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.
Syntax:
{
“Data”:[{
“key”:”value”,
“key”:value,
“key n “:”value”
},
. . .
. . .
{
“key”:”value”,
“key”:value,
“key n “:”value”
}]
}
Example: The JSON notation for student details is as follows.
{
“Student”:[{
“Name”:”Sravan”,
“Roll”:7058,
“subject”:”java”
},
{
“Name”:”Jyothika”,
“Roll”:7059,
“subject”:”SAP”
}]
}
Advantages:
- JSON does not use an end tag.
- JSON is a shorter format.
- JSON is quicker to read and write.
- JSON can use arrays.
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.
{
“Student”:[{
“Name”:”Sravan”,
“Roll”:7058,
“subject”:”java”
},
{
“Name”:”Jyothika”,
“Roll”:7059,
“subject”:”SAP”
}]
}
Use file_get_contents[] function to read JSON file into PHP. This function is used to read the file into PHP code.
Syntax:
file_get_contents[path, file_name]
- file_name is the name of the file and path is the location to be checked.
- Use json_decode[]function to decode to JSON file into array to display it.
It is used to convert the JSON into an array.
Syntax:
json_decode[$json_object, true]
- $json_object is the file object to be read.
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
{name:yekky}{name:mussie}{name:jessecasicas}
I am quite new at PHP. Do you know how I can use the decode the above JSON using PHP?
My PHP code
var_dump[json_decode['data.txt', true]];// var_dump value null
foreach [$data->name as $result] {
echo $result.'
';
}
asked Jun 10, 2011 at 22:34
1
json_decode takes a string as an argument. Read in the
file with file_get_contents
$json_data = file_get_contents['data.txt'];
json_decode[$json_data, true];
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].
[{"name":"yekky"}, {"name":"mussie"}, {"name":"jessecasicas"}]
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
[{"name":"yekky"},{"name":"mussie"},{"name":"jessecasicas"}]
[I dont know, where your quotes are gone, but json_encode[]
usually produce valid json]
And this is easy readable
$data = json_decode[file_get_contents['data.txt'], true];
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 json_decode
.
answered Jun 10, 2011 at 22:36
HalcyonHalcyon
56.6k10 gold badges87 silver badges126 bronze badges
$data = json_decode[file_get_contents['data.txt'], true];
But your JSON needs to be formatted correctly:
[ {"name":"yekky"}, ... ]
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:
$jsonBytes = file_get_contents['data.json'];
$data = json_decode[$jsonBytes, true];
/* Do something with data.
If you set the second argument of json_decode [as above], it's an array,
otherwise an object.
*/
answered Jun 10, 2011 at 22:38
phihagphihag
268k67 gold badges439 silver badges462 bronze badges
You have to read the file!
$json = file_get_contents['data.txt'];
var_dump[json_decode[$json, true]];
answered Jun 10, 2011 at 22:37
powtacpowtac
39.8k27 gold badges113 silver badges167 bronze badges