Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode returning null for UTF-8 charset

Tags:

json

php

I have a json file like this

{"downloads":[
    {
        "url":"arquivo1.pdf",
        "descricao":"árquivo 1"
    },
    {
        "url":"arquivo2.pdf",
        "descricao":"arquivo 2"
    }
]}

And I save it using UTF-8 encode via Notepad++.

Then I get the file content:

function getContent($name)
{
    $content = file_get_contents("configs/" . $name . ".json");
    $encoded = utf8_encode($content);
    return json_decode($encoded);
}

and json_decode returns null.

If I save the json file as ANSI then it works. But I'd like to save it as UTF-8.

like image 682
Fabricio Avatar asked Aug 09 '26 11:08

Fabricio


1 Answers

I suspect that the initial file is either already in UTF-8 or in a badly formatted type.

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

Bad encoding

You can check if your input content is already valid utf-8 by doing this:

$is_valid_utf8 = mb_check_encoding($content, 'utf-8'));

If it is, don't re-encode it.

The documentation has more to offer: http://php.net/mb-check-encoding

BOM

Or maybe Notepad++ sets a BOM which could confuse json_decode.

//Remove UTF-8 BOM if present, json_decode() does not like it.
if(substr($content, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) {
    $content = substr($content, 3);
}

see json_decode's documentation.

like image 181
greut Avatar answered Aug 11 '26 01:08

greut



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!