PHP json_decode() returns NULL with valid JSON?
It could be the encoding of the special characters. You could ask json_last_error() to get definite information.
Update: The issue is solved, look at the "Solution" paragraph in the question.
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)
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 despite apparently correct JSON response
Shouldn't the first test be negative (!is_null)? Only if it isn't null (and therefore a json object) should you then proceed.
Also, the second json_encode function you're passing a 'true' as the second argument which makes the returned object an associative array rather than an object.
The below alterations appeared to work for me.
if (!is_null(json_decode($result))){
$callObj = json_decode($result, true);
$callID = $callObj[0]['extension'];
}
else
{
echo "<BR>Could not decode response <BR>";
$callID = 'error';
}
return $callID;
json_decode return null with different qoute
JSON strings are quoted with double quotes "
. Single quotes '
(which are common in PHP) are not valid JSON. No discussion. Thus, the input ['foo','bar']
is not valid json and json_decode
correctly refuses to parse it.
Also see ECMA-404, which defines the JSON format:
A string is a sequence of Unicode code points wrapped with quotation marks (U+0022).1
If you're looking for something to transform your JSON-ish string (where does it come from? Fix the source of the invalid JSON, preferably) into valid JSON; str_replace('\'', '"', $jsonInput)
should work in simple cases.
1(U+0022
is the double quote "
)
json_decode returns null after storing 2 arrays in .json file - validation
One array under another is invalid JSON. You should use one root array and keep your users inside it (not sure if the contents inside the arrays make sense, but you get the idea):
[
[{"count":"3","name":"testnameone"},{"count":"5","name":"testnametwo"},{"count":"6","name":"testnamethree"},{"info":106}]
[{"count":"3","name":"testnamefour"},{"count":"5","name":"testnamefive"},{"count":"6","name":"testnamesix"},{"info":521}]
]
In other words, it should go as follows:
- User sends the form
- You read the contents of the file
- You decode it with
json_decode()
- You add to the array
- You encode it back with
json_encode()
- You save entire new JSON to the file, replacing the old contents
Related Topics
Replacing Accented Characters PHP
Understanding the "Post/Redirect/Get" Pattern
How to Set Order by Params Using Prepared Pdo Statement
Getting Dom Elements by Classname
Performance of Foreach, Array_Map With Lambda and Array_Map With Static Function
How to Become an Opencart Guru
MySQL Prepared Statements With a Variable Size Variable List
Json_Encode() Escaping Forward Slashes
Delete Directory With Files in It
How Does True/False Work in PHP
Select Count(*) from Table of MySQL in PHP
How to Echo a Variable With Single Quotes
PHP Fatal Error: Call to Undefined Function Curl_Init()
Automatic Post-Registration User Authentication
PHP Check If Date Between Two Dates