Unable to get json_decode() output - php

I am trying to decode a simple json string POSTed to a php file.
<?php
$jsonData = file_get_contents('php://input');
$data_back = json_decode($jsonData, true);
echo $jsonData;
echo $data_back;
?>
I tested this by POSTing this sample JSON
{"sensorid": SS0023, "x": 475, "y":212} with application/json Content-Type.
But it is returning the $jsonData values only. json_decode isn't doing anything and returning null.
Help ?

your json format must be like this
{"sensorid": "SS0023", "x": "475", "y":"212"}

Related

json_decode returns null and bool false

I have code:
<?php $json = '[{"title":" \\ud83c\\uddf7\\ud83c\\uddfa \\u0420\\u043e\\u0441\\u0441\\u0438\\u0439\\u0441\\u043a\\u043e\\u0435 \\u043a\\u0430\\u0437\\u0438\\u043d\\u043e #1
\\ud83c\\uddf7\\ud83c\\uddfa ","desc":"\\u0422\\u043e\\u043b\\u044c\\u043a\\u043e
\\u0437\\u0430\\u0440\\u0435\\u0433\\u0438\\u0441\\u0442\\u0440\\u0438\\u0440\\u0443\\u0439\\u0441\\u044f \\u0438 \\u0438\\u0433\\u0440\\u0430\\u0439
\\u043f\\u0435\\u0440\\u0432\\u044b\\u0439 \\u0440\\u0430\\u0437 \\u0437\\u0430 \\u043d\\u0430\\u0448
\\u0441\\u0447\\u0451\\u0442","icn":"https://1.mbvnclick1.com/ic?sid=2&data=0eikgji0Ck2EKXJkLTJfLie%2FKy%2FvWYZiVPrhxIOQsl6VkyioGiy%2B4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ%2BjltIeJAcUnB%2BTMvTjn%2BGs1lvh5bOIUUXYa0tIJCe%2BJe2LX38OpOLAJ%2B7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT%2FZ%2F3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA%2BiZlgYsEg%3D%3D","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b%2BWp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57%2B8L58uusM7pf8HpgSTreH8rjJIX%2BQEruq544CQQF%2FTNxTpCAesrBgQpkUOL76hSB%2F0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl%2FO6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn%2Boc1q7OZh2tb7pLxLU4il0MNlbTTRR%2FQJ3Ryj98kKbM2eOgq%2FVtMBpmy4huEGwavyp41rQdZTT%2Fjdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw%2F171wk2af2qmrmLi7e13XtfK%2BFpZltozDNAqS%2BDJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp%2FOFf6dOcERVM%2Ff%2FYBgEZ2E%2FpzuMZSywxPt49sveDcfOE%2F9LOjBu6%2BU1XymVQdknq%2B0MzuJAd6Eq8%2FH4q%2F%2B7dlgvivqQm30C%2FvhG%2FfGSYQPEY%2BHdzAJZ%2FStRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE%2B3QOCqafNoCq90X7U%2FLlvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG%2FqAZN5hzM0%3D","price":"0.0055"}]';
$json = json_decode($json, true); var_dump($json);
echo '<br>';
echo json_last_error();
?>
It returns null and if I check JSON for errors it returns bool(false). What is wrong? Online decoders are decoding json normally. JSON is advertisment I'm using for test.
I've been searching for issue for hours. I've deleted urls from json, checking if another jsons are working on site (they work). IDK what to do
$json = preg_replace('/[[:cntrl:]]/', '', $json); $array = json_decode($json, true);
I should preg_replace before decoding JSON

Php json _decode function returns null on long data

I pull data from a json file on the remote server. This json file has 97000 Lines of json code. It returns null when I decode the Json file. When I debug the Json errors, I see that there is no error.
Json file : https://opendata.ecdc.europa.eu/covid19/casedistribution/json/
$json = file_get_contents("https://opendata.ecdc.europa.eu/covid19/casedistribution/json/");
$json = json_decode($json, true);
var_dump($json); // Return Null
But when I decode another json file there is no error
$json = file_get_contents("https://randomuser.me/api/");
$json = json_decode($json, true);
var_dump($json); // Return Array
Could this be due to the size of the data?
Thanks for advance
The file starts with a BOM, which is a syntax error for json_decode.
Ultimately this should be fixed by the host, as a workaround you can strip the first three bytes:
if (substr($json, 0, 3) == "\xEF\xBB\xBF") {
$json = substr($json, 3);
}

Problems parsing JSON with PHP (CURL)

I am trying to access the value for one of the currencies (e.g. GBP) within the "rates" object of the following JSON file:
JSON file:
{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}
This was my approach:
PHP (CURL):
$url = ...
// initialize CURL:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// get the (still encoded) JSON data:
$json = curl_exec($ch);
curl_close($ch);
// store decoded JSON Response in array
$exchangeRates = (array) json_decode($json);
// access parsed json
echo $exchangeRates['rates']['GBP'];
but it did not work.
Now, when I try to access the "timestamp" value of the JSON file like this:
echo $exchangeRates['timestamp'];
it works fine.
Any ideas?
Try removing (array) in front of json_decode and adding true in second parameter
Here is the solution.
$exchangeRates = (array) json_decode($json,TRUE);
https://ideone.com/LFbvUF
What all you have to do is use the second parameter of json_decode function.
This is the complete code snippet.
<?php
$json='{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}';
$exchangeRates = (array) json_decode($json,TRUE);
echo $exchangeRates["rates"]["GBP"];
?>

How to output json data in array using curl

I have code here that will get json data using curl. Now I want to echo all data. This will show no output.
<?php
$json = file_get_contents('myurl');
$data = json_decode($json);
print_r($data);
?>
This is json coming from my url:
({"Response_Code":"0000","ResultMobilePrefix":["0917","0905","0906","0915","0916","0926","0927","0937","0935","0817","0936","0922","0923",
"0932","0933","0934","0942","0943","0907","0908","0909","0910","0912","0918","0919","0920","
0921","0928","0929","0930","0938","0939","0948","0949","0925","0989","0999","0947","0998","
0946","0975","0977"]});
All you need is
echo json_encode($json);
Use true to convert it into array;
$data = json_decode($json,true);
echo '<pre>';
print_r($data);
for displaying json in format use json_encode() and for proper format use JSON_PRETTY_PRINT
header('Content-type: application/json');
echo json_encode($json,JSON_PRETTY_PRINT);
you need to get rid of the bracket and semicolon on the JSON file first before you can use the json_decode();
<?php
$json = file_get_contents('myurl');
//remove the brackets
$json = str_replace("(", "", $json);
$json = str_replace(")", "", $json);
//remove the semicolon
$json = str_replace(";", "", $json);
$data = json_decode($json);
print_r($data);
?>
this is a bit ugly, but hope you get the idea, you need to remove that character first
I just want to add to what Manish has explained above. This is what PHP Docs say about the second parameter:
When TRUE, returned objects will be converted into associative arrays.
else you will simply get a stdclass object
The problem is the URL does not return a valid Json
you could simply try
var_dump($data);
this will return null because it's not a valid json see json_decode
this will work
$json = file_get_contents('myurl');
$json = preg_replace('/[ ]{2,}|[\t\n\r\(\)\;]/', '', trim($json));
$data = json_decode($json);
print_r($data);

get Specific JSON Response Value

json response as below:
{"Success":true,"ErrorCode":0,"UserInformation":{"UserID":"19"}......
how do I get the UserID value by using json_decode?
I tried the below code with no luck.
$Response = json_decode($Response[2]);
echo $Response[0][0][0];
This should work:
<?php
$json = '{"Success":true, "ErrorCode":"0","UserInformation":{"UserID":"19"}}';
$response = json_decode($json, true);
echo $userId = $response["UserInformation"]["UserID"];
?>
Please check the PHP documentation of json_decode at http://php.net/json_decode
You can use object as well if you skip the second parameter of the json_decode function.

Categories