Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK

Tags:

json

php

I got a very strange problem.

I have a JSON webservice.

When i check it with this website http://www.freeformatter.com/json-formatter.html#ad-output

Everything is OK.

But when i load my JSON with this code :

  $data = file_get_contents('http://www.mywebservice');  if(!empty($data)) {      $obj = json_decode($data);   switch (json_last_error()) {     case JSON_ERROR_NONE:         echo ' - JSON_ERROR_NONE';     break;     case JSON_ERROR_DEPTH:         echo ' - JSON_ERROR_DEPTH';     break;     case JSON_ERROR_STATE_MISMATCH:         echo ' - JSON_ERROR_STATE_MISMATCH';     break;     case JSON_ERROR_CTRL_CHAR:         echo ' -  JSON_ERROR_CTRL_CHAR';     break;     case JSON_ERROR_SYNTAX:         echo "\r\n\r\n - SYNTAX ERROR \r\n\r\n";     break;     case JSON_ERROR_UTF8:         echo ' - JSON_ERROR_UTF8';     break;     default:         echo ' - Unknown erro';     break; } 

I got the error : SYNTAX ERROR

WHICH IS NOT HELP FULL AT ALL.

It is a nightmare.

I see that with PHP 5.5 i could use this function : http://php.net/manual/en/function.json-last-error-msg.php

(but i did not succeed to install PHP 5.5 yet, and i m not sure this function will give me more detail)

like image 201
Jean François Manatane Avatar asked Jun 20 '13 17:06

Jean François Manatane


People also ask

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

How check data is JSON or not in PHP?

Simple function to validate JSON. If you have to validate your JSON in multiple places, you can always use the following function. function is_valid_json( $raw_json ){ return ( json_decode( $raw_json , true ) == NULL ) ? false : true ; // Yes!

What Is syntax error malformed JSON?

The malformed JSON error means that the format of the API call is incorrect.


2 Answers

I faced the same issue, actually there are some hidden characters unseen and you need to remove it. Here's a global code that works for many cases:

<?php $checkLogin = file_get_contents("http://yourwebsite.com/JsonData");  // This will remove unwanted characters. // Check http://www.php.net/chr for details for ($i = 0; $i <= 31; ++$i) {      $checkLogin = str_replace(chr($i), "", $checkLogin);  } $checkLogin = str_replace(chr(127), "", $checkLogin);  // This is the most common part // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level) // here we detect it and we remove it, basically it's the first 3 characters  if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) {    $checkLogin = substr($checkLogin, 3); }  $checkLogin = json_decode( $checkLogin ); print_r($checkLogin); ?> 
like image 95
Kris Khairallah Avatar answered Sep 27 '22 03:09

Kris Khairallah


Removing the BOM (Byte Order Mark) is often-times the solution you need:

function removeBOM($data) {     if (0 === strpos(bin2hex($data), 'efbbbf')) {        return substr($data, 3);     }     return $data; } 

You shouldn't have a BOM, but if it's there, it is invisible so you won't see it!!

see W3C on BOM's in HTML

use BOM Cleaner if you have lot's of files to fix.

like image 36
Greg Rundlett Avatar answered Sep 27 '22 03:09

Greg Rundlett