3

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.

1
  • if the file is already in utf-8 you don't have to re-encode it a second time. Commented Feb 10, 2012 at 17:45

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Notepad++ has an option to enconde Without BOM. Now I'm using that. Before i was using the short option. thats why it didn't work indeed. thank you. I dont know what is this BOM for.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.