Decode json data from .json file in php - php

I am trying to output some json data from a json file via php but it does not seem to work. I tried this:
<?php
$jsonFile = file_get_contents('dataset/dataset.json');
$data = json_decode($jsonFile, true);
echo $data->{'data'}[0]->{'letter'}
?>
The json file is following:
{
"data":[
{
"letter":"A",
"blocks":{
"1":"0",
"2":"0",
"3":"0",
"4":"0",
"5":"0"
}
}
]}
Basically it should output the letter "A" but it outputs nothing. What did I do wrong?
Thanks
P.S. I tried to do it like here: How to process JSON in PHP? but it does not work.

After json_decode($jsonFile, true) your data is in array. So you should not access using object. Access data by array index. Try this..
echo $data['data'][0]['letter'];
More about json_decode()

This says, you get an array (the true parameter):
$data = json_decode($jsonFile, true);
You can see this if you do this:
print_r($data);
Try this:
echo $data['data'][0]['letter'];

Related

Get values from JSON array using PHP

I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>

How to display JSON array

So i have written this code to get a json from API.AI. i want to know how to echo out the decoded JSON file on this form. Currently the decoded JSON is being saved in a new file JSON.php, but instead of saving i want to know how to view it?
$data = json_decode(file_get_contents("php://input"), true);
file_put_contents("JSON.php", print_r($data, true));
As comments state you can use var_dump to view the rough data but if you actually wanna use it for something you might wanna loop over the object and extracte properties.
like so:
foreach($data as $theData)
{
echo $theData->property;
}
Regards
do like this:-
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
print_r($request);
or you can do :- $request->indexname

How to get value from JSON encode in PHP?

i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.

PHP Using ($_GET['url']) and then displaying that json data?

<?php
function get_stuff()
{
($_GET['http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****']);
echo $_GET;
}
get_stuff();
?> /* Yes, Pointless closing tag, I'm cool like that. */
For some reason when I run this code the output I'm getting is "Array" and I can't figure out why? I know it is an array that I'm getting from the URL but I thought it would just print in whatever format its in? Am I missing something?
Thanks In advance!
You cannot pass website URL to $_GET.
You should use file_get_contents()
$content = file_get_contents('http://YOUR_URL');
If you have valid json then you can convert it to an array.
$data = json_decode($content, true);
then print it,
echo '<pre>'; print_r($data);
The Message of OP when I run this code the output I'm getting is
"Array".
You need to use file_get_contents to read a file from a remote server. If your file response an array then you have to use print_r or var_export or var_dump. Or if your file response is a string(json) then you need to store it in a variable and apply a decode method.
function get_stuff(){
$response = file_get_contents('http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****');
print_r($response); // if array
$arr = json_decode($response); // decode of json
print_r($arr);
}
get_stuff();
I think you will understand what i mean. Let me know if you are useful or need some help.

How do I parse a JSON object in PHP?

I've got a JSON object that I'm sending to a PHP script and I'm having trouble parsing the JSON. Here's the POST request:
http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}
And here's my PHP function, which obviously doesn't work:
$data = $_GET['data'];
$obj = json_decode($data);
echo $obj->Name;
die();
The end goal is to extract the name "Mike" from the URL string. Any suggestions?
Try taking a look at what PHP is outputting from json_decode():
$data = $_GET['data'];
$obj = json_decode($data);
var_dump($obj);
Your code itself works fine: http://ideone.com/0jsjgT
But your query string is missing the data= before the actual JSON. This:
http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}
should be this:
http://mywebsite.com?action=somefunction&data={%22id%22:1,%22Name%22:%22Mike%22}
you should do
echo $obj->{'Name'};
This also is a duplicate question of Echo data json by json_decode

Categories