Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json_decode JSON_ERROR_SYNTAX

Tags:

json

php

I try to load and parse JSON file from this link

But I have JSON_ERROR_SYNTAX problem and Invalid argument supplied for foreach(). Why it's happen?

[{ "Manufacturer": "Toyota", "Sold": 1200, "Month": "2012-11" }, { "Manufacturer": "Ford", "Sold": 1100, "Month": "2012-11" }, { "Manufacturer": "BMW", "Sold": 900, "Month": "2012-11" }, { "Manufacturer": "Benz", "Sold": 600, "Month": "2012-11" }, { "Manufacturer": "GMC", "Sold": 500, "Month": "2012-11" }, { "Manufacturer": "HUMMER", "Sold": 120, "Month": "2012-11" }]

<?php
$url = "http://www.pureexample.com/backend/data/car-sale.json";
$url = file_get_contents($url);
print_r($url);
$url = stripslashes($url);
print_r($url);
$url = str_replace("\n", "", $url);
print_r($url);
$url = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($url));
print_r($url);
$url = json_decode($url, true);
// Add this switch to your code
switch (json_last_error())
{
    case JSON_ERROR_NONE:
        echo ' - No errors';
        break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
        break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
        break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
        break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
    default:
        echo ' - Unknown error';
        break;
}


foreach($koyim as $data){
echo $data['Manufacturer'];
echo $data['Sold'];
echo $data['Month'];
echo "<br/>";
}
?>

1 Answers

The link you provided serves the JSON in UTF-8 with a Byte Order Mark. Apparently json_decode() does not deal well with these three extraneous characters. The solution would be to strip the BOM:

<?php

//see https://stackoverflow.com/a/32185872/500890
function removeBomUtf8($s){
   if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
        return substr($s,3);
    }else{
        return $s;
    }
}

$url = "http://www.pureexample.com/backend/data/car-sale.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);

//Recovered data
echo "<pre>" . print_r($decoded, TRUE);

I took this solution to strip the BOM: https://stackoverflow.com/a/32185872/500890, although there are a ton of them floating on the Internet. The main idea is to strip the first three specific characters if they are indeed a BOM.

like image 125
lampyridae Avatar answered Mar 08 '26 18:03

lampyridae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!