PHP json_decode() returns NULL with seemingly valid JSON?
This error means that your JSON string is not valid JSON!
Enable throwing exceptions when an error happens and PHP will throw an exception with the reason for why it failed.
Use this:
$json = json_decode($string, null, 512, JSON_THROW_ON_ERROR);
Php - json_decode returns null
<?php
$data='{
"id": "1",
"fields": [{
"id": "1",
"value": "asdasd"
}, {
"id": "2",
"value": "asdasd"
}, {
"id": "3",
"value": "asdasd"
}]
}';
$dataNew=json_decode($data,true);
echo '<pre>';
print_r($dataNew);
Your json was not valid. The json-keys need to be inside double-quotes. After that json_decode will work fine.
Output is:
Array
(
[id] => 1
[fields] => Array
(
[0] => Array
(
[id] => 1
[value] => asdasd
)
[1] => Array
(
[id] => 2
[value] => asdasd
)
[2] => Array
(
[id] => 3
[value] => asdasd
)
)
)
json_decode returns NULL after webservice call
EDIT:
Just did some quick inspection of the string provided by the OP. The small "character" in front of the curly brace is a UTF-8 B(yte) O(rder) M(ark) 0xEF 0xBB 0xBF
. I don't know why this byte sequence is displayed as
here.
Essentially the system you aquire the data from sends it encoded in UTF-8 with a BOM preceding the data. You should remove the first three bytes from the string before you throw it into json_decode()
(a substr($string, 3)
will do).
string(62) "{"action":"set","user":"123123123123","status":"OK"}"
^
|
This is the UTF-8 BOM
As Kuroki Kaze discovered, this character surely is the reason why json_decode
fails. The string in its given form is not correctly a JSON formated structure (see RFC 4627)
json_decode() returns null, even though file_get_contents() works perfectly, even though json is valid
You can do it like:
//storing json contents in a variable.
$json_contents = file_get_contents('../../Datafiles/allThreads.txt');
//decode json
$currentThreadasList = json_decode($json_contents, TRUE)
Edit-1:
Have you tried the following:
$currentThreadasList = json_decode(file_get_contents('../../Datafiles/allThreads.txt'), TRUE);
json_decode returns null and bool false
$json = preg_replace('/[[:cntrl:]]/', '', $json); $array = json_decode($json, true);
I should preg_replace
before decoding JSON
json_decode() returns null issues
What a HORRENDOUS debug session.. well there's good news.. I figured it out..
I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle "
, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:
$json = str_replace('"', '"', $json);
Now I thought they were the same.. maybe someone can enlighten me..
Related Topics
How to Connect PHP with Microsoft Access Database
How to Write to Error Log File in PHP
How to Check for a Specific Type of Object in PHP
PHP MySQL - Insert New Record into Table with Auto-Increment on Primary Key
Convert to Date Format Dd/Mm/Yyyy
Try Catch Cannot Work with Require_Once in PHP
Soapfault Exception: Could Not Connect to Host
Difference Between File, File_Get_Contents, and Fopen in PHP
Curl Requires Curlopt_Ssl_Verifypeer=False
How to Search Through a JSON Array in PHP
Make Multiple Files to Force-Download
Set Product Sale Price Programmatically in Woocommerce 3
How to Avoid Resending Data on Refresh in PHP
Secure User Image Upload Capabilities in PHP
MySQL or PHP Is Appending a  Whenever the £ Is Used
Woocommerce - How to Create Multiple Single Product Template Based on Category