tags:

views:

459

answers:

1

I have this JSON object stored on a plain text file:


{
    "MySQL": {
        "Server": "(server)",
        "Username": "(user)",
        "Password": "(pwd)",
        "DatabaseName": "(dbname)"
    },
    "Ftp": {
        "Server": "(server)",
        "Username": "(user)",
        "Password": "(pwd)",
        "RootFolder": "(rf)"
    },
    "BasePath": "../../bin/",
    "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 JSONLint and it's perfectly valid.

What's wrong here?

Solution

Looking for answers on Google, I got back to SO: http://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!

+2  A: 

It could be the encoding of the special characters. You could ask json_last_error() to get definite information.

Update: The issue is solved, look at the "Solution" paragraph in the question.

Pekka
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 Alejandro
Replaced the special chars with HTML entities. Still the same result.
Joel Alejandro
@Joel hang on, I'll test it on 5.3
Pekka
"No error" - wtf?!?
Pekka
It's not an encoding issue either. I tried encoding/decoding in UTF-8, no changes. Time to switch to INI format, maybe?
Joel Alejandro
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: http://de.php.net/json_decode maybe something helps.
Pekka
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.
konforce
Checked the file encoding, it is UTF-8. And as my application works all on UTF-8, there's no need for decoding.
Joel Alejandro
Problem has been solved! I'm gonna accept your answer though, for giving me some of your time =)
Joel Alejandro
@Joel cheers. What was it in the end?
Pekka
@Pekka Looking for answers on Google, I got back to SO: http://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 Alejandro
@Joel aah, very good to know!
Pekka
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
@jmucchiello absolutely true.
Pekka