json_decode returns null and bool false - php

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

Related

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

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

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"];
?>

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.

PHP has to save JSON String first and open it again, to work with it

I'm having the strangest behavior and I was working on it for a few hours now. I'm sending a large and complex JSON string via AJAX to my server and when I decode it, I cannot access its elements. But when I go ahead and save the decoded JSON string to a file and open it again, I am suddenly able to work with its elements. I just cannot explain this behavior. Here's my code.
This does not work
header('Access-Control-Allow-Origin: *');
$json = $_POST['json'];
$obj = json_decode($json, true);
// At this point I cannot work with the $obj elements
This works
header('Access-Control-Allow-Origin: *');
$json = $_POST['json'];
$data = json_decode($json, true);
file_put_contents( 'test.txt', $data);
$file = file_get_contents('test.txt');
$obj = json_decode($file);
// At this point I can work with the $obj elements
Please Note, that I need the header, because I get the JSON from a different server.
It looks like your json is double encoded, that is it works when you decode it twice. Try
$obj = json_decode(json_decode($json, true));
in your first trial you used:
$obj = json_decode($json, true);
The true is converting to array not object

Categories