Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams "NotesAppPath": "notas", "SearchAppPath": "buscar", "BaseUrl": "http:\/\/montemaiztusitio.com.ar", "InitialExtensions": [ "nem.mysqlhandler", "nem.string", "nem.colour", "nem.filesystem", "nem.rss", "nem.date", "nem.template", "nem.media", "nem.measuring", "nem.weather", "nem.currency" "MediaPath": "media", "MediaGalleriesTable": "journal_media_galleries", "MediaTable": "journal_media", "Journal": { "AllowedAdFileFormats": [ "flv:1", "jpg:2", "gif:3", "png:4", "swf:5" "AdColumnId": "3", "RSSLinkFormat": "%DOMAIN%\/notas\/%YEAR%-%MONTH%-%DAY%\/%TITLE%/", "FrontendLayout": "Flat", "AdPath": "ad", "SiteTitle": "Monte Maíz: Tu Sitio", "GlobalSiteDescription": "Periódico local de Monte Maíz.", "MoreInfoAt": "Más información aquí, en el Periódico local de Monte Maíz.", "TemplatePath": "templates", "WeatherSource": "accuweather:SAM|AR|AR005|MONTE MAIZ", "WeatherMeasureType": "1", "CurrencySource": "cotizacion-monedas:Dolar|Euro|Real", "TimesSingular": "vez", "TimesPlural": "veces"

When I try to decode it with json_decode() , it returns NULL. Why? The file is readable (I tried echoing file_get_contents() and it worked ok).

I've tested JSON against http://jsonlint.com/ and it's perfectly valid.

What's wrong here?

Also note this can happen with other invalid characters in the middle of the file. I just had json_decode() returning null because the string contained one of those special en-dashes, probably pasted from MS Word, and then maybe mis-encoded. To identify potential problem characters, open the JSON file (I used in Notepad++), change the encoding (without converting), and save as a copy. Then diff the two files (I used WinMerge). LinusR Jan 17, 2012 at 17:15 (Windows Notepad issue) Please, consult this, I shared the problem too and it fixed it: stackoverflow.com/questions/10290849/… Felix Aballi Sep 19, 2014 at 14:49 For me, it wasn't anything special, just an extra comma in the end of an object's element. Take away : Anything which makes your JSON inconsistent, is going to throw an error. Bonus tip: don't trust jsonviewer.stack.hu Use something like jsonlint Aman Alam Oct 24, 2016 at 11:30 I have used this and got the array but my language specific characters (ş,ç,ö,..) has been deleted too. zkanoca Dec 17, 2014 at 10:56 This is not correct if the json data is UTF-8 encoded (or any UTF encoding I guess). It will remove valid UTF-8-encoded data. It will probably work as long as the file only contains english language, but that's always a risky assumption. I wouldn't use this. DaedalusAlpha Oct 13, 2015 at 15:28 With this it works but without it doesn't, even tho the two string are identical, am I missing something? Rudie Visser Aug 25, 2016 at 10:03 I've been using the special characters since I started the application and there were no problems before. Locally, the JSON decoding works perfectly. On my server, it doesn't. And I can't call json_last_error() because it's PHP 5.2.9. That function appears on PHP 5.3.0. Joel A. Villarreal Bertoldi Mar 9, 2010 at 15:54 Nah, this should work. I can't do more testing right now, if I get to it later I'll post here. There are also a few hints in the user contributed notes: de.php.net/json_decode maybe something helps. Pekka Mar 9, 2010 at 16:09 For me, on PHP 5.3, it works fine when the text is encoded in UTF-8. But if I pass the text through utf8_decode() first, then json_decode() silently fails. Matthew Mar 9, 2010 at 16:15 @Pekka Looking for answers on Google, I got back to SO: stackoverflow.com/questions/689185/json-decode-returns-null-php . My JSON file had the UTF BOM sequence (some binary chars that shouldn't be there), thus, breaking the JSON structure. Went to Hex Editor, erased the bytes. Everything's back to normal. Why has this happened? Because I edited the file using Micro$oft Windows' Notepad. Terrible idea! Joel A. Villarreal Bertoldi Mar 9, 2010 at 17:59 This should be reported as a bug to the PHP folks. If the BOM was valid UTF8, it should not choke on it silently. jmucchiello Mar 9, 2010 at 18:34

If you check the the request in chrome you will see that the JSON is text, so there has been blank code added to the JSON.

You can clear it by using

$k=preg_replace('/\s+/', '',$k);

Then you can use:

json_decode($k)

print_r will then show the array.

Did it for me!! A simple tweak I made is adding a space in the replace, am using this, and it seems to replace my space as well. works fine now. $k=preg_replace('/\s+/', ' ',$k); Kash Dec 4, 2018 at 14:35 The problem is, this removes every single space, making english text all stuck together isn't it ? CodeGuru Dec 5, 2019 at 3:02

Maybe some hidden characters are messing with your json, try this:

$json = utf8_encode($yourString);
$data = json_decode($json);

I had the same problem and I solved it simply by replacing the quote character before decode.

$json = str_replace('"', '"', $json);
$object = json_decode($json);

My JSON value was generated by JSON.stringify function.

In this case the function htmlspecialchars has possibly being used and the JSON cannot be parsed anymore. To reverse it there is the function "htmlspecialchars_decode" instead of manually replacing " – Davy Nov 17, 2019 at 17:48

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);

For me the php function stripslashes() works when receiving json from javascript. When receiving json from python, the second optional parameter to the json_decode call does the trick since the array is associative. Workes for me like a charm.

$json = stripslashes($json); //add this line if json from javascript
$edit = json_decode($json, true); //adding parameter true if json from python
$json[] = '{"Organization": "PHP Documentation Team"}';
// An invalid json string which will cause an syntax 
// error, in this case we used ' instead of " for quotation
$json[] = "{'Organization': 'PHP Documentation Team'}";
foreach ($json as $string) {
    echo 'Decoding: ' . $string;
    json_decode($string);
    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;
    echo PHP_EOL;
                Since PHP 5.5.0 you can use the method  json_last_error_msg to get a somewhat helpful message
– Tomás Cot
                Apr 7, 2021 at 14:54

Just thought I'd add this, as I ran into this issue today. If there is any string padding surrounding your JSON string, json_decode will return NULL.

If you're pulling the JSON from a source other than a PHP variable, it would be wise to "trim" it first:

$jsonData = trim($jsonData);

The most important thing to remember, when you get a NULL result from JSON data that is valid is to use the following command:

json_last_error_msg();
var_dump(json_last_error_msg());
string(53) "Control character error, possibly incorrectly encoded"

You then fix that with:

$new_json = preg_replace('/[[:cntrl:]]/', '', $json);

It's probably BOM, as others have mentioned. You can try this:

    // BOM (Byte Order Mark) issue, needs removing to decode
    $bom = pack('H*','EFBBBF');
    $response = preg_replace("/^$bom/", '', $response);
    unset($tmp_bom);
    $response = json_decode($response);

This is a known bug with some SDKs, such as Authorize.NET

Thank-you TomoMiha. This is exactly what fits all my problems with MySQL containing special characters and when converted by json_decode, that particular field was yielded = null.... – KLL Jul 5, 2016 at 21:04

So, html_entity_decode() worked for me. Please try this.

$input = file_get_contents("php://input");
$input = html_entity_decode($input);
$event_json = json_decode($input,true);

It took me like an hour to figure it out, but trailing commas (which work in JavaScript) fail in PHP.
This is what fixed it for me:

str_replace([PHP_EOL, ",}"], ["", "}"], $JSON);
  • I also face the same issue...
  • I fix the following steps... 1) I print that variable in browser 2) Validate that variable data by freeformatter 3) copy/refer that data in further processing
  • after that, I didn't get any issue.
  • In my case , i was facing the same issue , but it was caused by slashes inside the json string so using

    json_decode(stripslashes($YourJsonString))
    
    json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $YourJsonString), true );
    

    If the above doesnt work, first replace the quotes from html quote , this might be happening if you are sending data from javascript to php

        $YourJsonString = stripslashes($YourJsonString);
        $YourJsonString = str_replace('"', '"', $YourJsonString);
        $YourJsonString = str_replace('["', '[', $YourJsonString);
        $YourJsonString = str_replace('"]', ']', $YourJsonString);
        $YourJsonString = str_replace('"{', '{', $YourJsonString);
        $YourJsonString = str_replace('}"', '}', $YourJsonString);
        $YourJsonObject = json_decode($YourJsonString);
    

    Will solve it,

    This happen because you use (') insted {") in your value or key.

    Here is wrong format.

    {'name':'ichsan'}
    

    Thats will be return NULL if you decode them.

    You should pass the json request like this.

    {"name":"ichsan"}
    

    Just save some one time. I spent 3 hours to find out that it was just html encoding problem. Try this

    if(get_magic_quotes_gpc()){
       $param = stripslashes($row['your column name']);
    }else{
      $param = $row['your column name'];
    $param = json_decode(html_entity_decode($param),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;
    print_r($param);
    

    I've solved this issue by printing the JSON, and then checking the page source (CTRL/CMD + U):

    print_r(file_get_contents($url));
    

    Turned out there was a trailing <pre> tag.

    1. your json string dont have any unknowns characters

    2. json string can view from online json viewer (you can search on google as online viewer or parser for json) it should view without any error

    3. your string dont have html entities it should be plain text/string

    for explanation of point 3

    $html_product_sizes_json=htmlentities($html);
        $ProductSizesArr = json_decode($html_product_sizes_json,true);
    

    to (remove htmlentities() function )

    $html_product_sizes_json=$html;
        $ProductSizesArr = json_decode($html_product_sizes_json,true);
    

    For my case, it's because of the single quote in JSON string.

    JSON format only accepts double-quotes for keys and string values.

    Example:

    $jsonString = '{\'hello\': \'PHP\'}'; // valid value should be '{"hello": "PHP"}'
    $json = json_decode($jsonString);
    print $json; // null
    

    I got this confused because of Javascript syntax. In Javascript, of course, we can do like this:

    let json = {
        hello: 'PHP' // no quote for key, single quote for string value
    // OR:
    json = {
        'hello': 'PHP' // single quote for key and value
    

    but later when convert those objects to JSON string:

    JSON.stringify(json); // "{"hello":"PHP"}"
    

    I had exactly the same problem But it was fixed with this code

    $zip = file_get_contents($file);
    $zip = json_decode(stripslashes($zip), true);
    $json_url = "http://api.testmagazine.com/test.php?type=menu";
    $json = file_get_contents($json_url);
    $json=str_replace('},
    ]',"}
    ]",$json);
    $data = json_decode($json);
    echo "<pre>";
    print_r($data);
    echo "</pre>";