Fabricio
Fabricio

Reputation: 7935

json_encode returning null for UTF-8 charset

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.

Upvotes: 3

Views: 7624

Answers (3)

greut
greut

Reputation: 4373

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.

Upvotes: 10

entropid
entropid

Reputation: 6239

It isn't working because your file is already in UT8, and when you encode it again using utf8_encode(), PHP assumes your string is an ISO-8859-1 string and therefore breaks it.

Upvotes: 0

Boris Belenski
Boris Belenski

Reputation: 1412

json_decode woks fine with UTF-8 without BOM. Do you have any particular reason to use BOM?

If you convert your json file to UTF-8 without BOM you will not need to encode the content later with utf8_encode.

Upvotes: 0

Related Questions