php json_decode returns NULL with syntax error
NickName:user2073143 Ask DateTime:2016-03-04T03:22:49

php json_decode returns NULL with syntax error

At the risk of duplication, here is my very own struggle with json_decode(). I've been through many if not all the posts on this and nothing has worked. So here goes:

Here is a sample of my json variable's content:

{"id":"212","game":"36","player":"1","currentChallenge":"3","startDate":"1456575719","endDate":"1456575918","attempts":"0","completed":"1"}

I checked it on http://jsonlint.com/ and it's good I am running these filters as per other forum suggestions:

$response = utf8_encode($response);
$response = str_replace('"', '"', $response);

And here is my decode line:

var_dump(json_decode($response, true));

I still get a NULL response and a syntax error (json_last_error()).

This data is coming from my own database and api. I tried inputting manually to the database and querying that, figuring I could get rid of any problems created by the input process. No luck there. And I have also sampled various tables to be sure it is not a problem related to the database setup.

And here is all my code:

if (isset($_POST['submit']) ) {

    $table = $_POST['table'];
    $id = $_POST['id'];
    $url = 'http://example.com/api.php/' . $table . '/' . $id;

    $client = curl_init($url);
    curl_setopt($client,CURLOPT_RETURNTRANSFER,true);

    $response = curl_exec($client);
    if ($response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }

    $response = utf8_encode($response);
    $response = str_replace('"', '"', $response);
    echo $response;

    echo '<br />Result of json_decode: '; 
    var_dump(json_decode($response, true));

    $json_errors = array(
        JSON_ERROR_NONE => 'No error has occurred',
        JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
        JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
        JSON_ERROR_SYNTAX => 'Syntax error',
    );
    echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;

}

Thank you!

Copyright Notice:Content Author:「user2073143」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35781163/php-json-decode-returns-null-with-syntax-error

More about “php json_decode returns NULL with syntax error” related questions

php json_decode returns NULL with syntax error

At the risk of duplication, here is my very own struggle with json_decode(). I've been through many if not all the posts on this and nothing has worked. So here goes: Here is a sample of my json

Show Detail

PHP json_decode returns null value because of syntax error

$foo_json = '{"hoge":"ywwxEu|`tYdBeARGJ?nJ~BHHmDjX|PdEl@Rj@\\XVtKtK\\VbKnFrIbEzEbBnEbBfDfAxBz@LJjBl@jEzAfZzKDPY|DUdCGbAFL?|AD~AJ\\LNPJ~B|@p@Ll@FRDd@RvBnAfA`@dAl@^NhAVd@@p@?h@H`@Pv@l@TR^f@`Rd@xAFdEV...

Show Detail

php json_decode() returns null

I have this string: "{\"time\":1327220746000,\"long\":14.382638525754006,\"acc\":35,\"alt\":326,\"lat\":50.07442396194751}" and while trying to parse it with json_decode in my p

Show Detail

json_decode return null with different qoute

json_decode('["foo","bar"]', true), this works, but this return NULL, json_decode("['foo','bar']", true). The json_last_error() outputs 4, JSON_ERROR_SYNTAX. I've checked some answe

Show Detail

PHP json_decode returns null on valid string

I have the following code: $option = $this-&gt;request-&gt;post['option']; var_dump($option); echo "&lt;br&gt;"; var_dump(json_decode($option)); The dumps show: string(118) &q

Show Detail

PHP json_decode returns null, even though the json is valid

&lt;?php require_once('config.php'); //connection info $array = array(); // var_dump($array); $json = file_get_contents("php://input"); if (empty($json)){ //th

Show Detail

json_decode() returns null issues

I've an issue with my JSON. It works returns correctly in PHP 5.3 (so I can't use json_last_error()), and it returns successfully when I copy string explicitly into json_decode (json_decode('{...}'...

Show Detail

Problem with json_decode - Syntax error, malformed JSON

I'm receiving a json array from php as the return of curl_exec in PHP (first json PHP -> python, that returns another json), and decode fails due to bad syntax. The piece of API code: if($_GET['u...

Show Detail

PHP - json_decode() returns null when using echo

I am using the following script to decode a json. Although the var_dump($obj) returns result (similar to one in here Example #1), the echo line doesn't return any result, as if the $obj being null....

Show Detail

PHP: json_decode returns NULL (Paytm checksum generation)

I am trying to generate checksum for Paytm payment gateway using php. &lt;?php // following files need to be included require_once("./lib/config_paytm.php"); require_once("./lib/encdec_paytm.php"...

Show Detail