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.

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

$json = file_get_contents('my_data.json');

$json_data = json_decode($json,true);

print_r($json_data);

?>

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.'
'; }

Php decode json from file

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

Php decode json from file

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

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);