Skip to Content

Frustrations with IE and JSON code

I write a fair bit of JavaScript. I also run into situations frequently where I need to pass data to or from a server to the web page via Ajax. jQuery helps a lot with this, but there I keep running into one tiny little snag with JSON that frustrates me no end.

Internet Explorer (6 or 7) doesn't like this JSON code:

 { "name": "bob", "phone" : "555-5555", "age" : 55, }

In Firefox this works absolutely fine. No errors. From what I understand this is the case with any other browser. Except Internet Explorer. Notice that last comma? The one after the age property. IE thinks this is a syntax error and dutifully reports it as such. Well, as "dutifully" as IE *ever* reports a JavaScript error. This is such a subtle bug any error report you get from IE is more or less useless - other than to say it found what it thinks is a bug.

Logically thinking, this extra comma would suggest a null property - which should just be ignored. And some browsers treat it as such. But not IE.

What this means is that anyplace we are creating a JSON object string in code, we have to watch for that last comma. If you are creating an array of values/objects, you now need to check to see if you are at the last element in the element list and NOT include the comma.

In some ways, I can appreciate the fact that IE follows the standards properly here. In other ways I can't believe they wouldn't throw in a reasonable check for this sort of thing. But this is IE with it's long long history of oddities. So I'm not really surprised.

(ok, I know this is such a minor thing, but I felt the need to vent. This one has bit me many many times.)

Oh, while we are talking about JSON, just a reminder - make sure all your property names are quoted within your JSON string. This is what the standard calls for and some browsers/interpreters absolutely expect it. Some will let you get away without the quotes, but it's probably a better practice to follow the standard....