comments 2

Trailing Comma Within JSON’s Array

I was debugging an AJAXy app which uses JSON as the data format, and it was fine with Firefox but not with Internet Explorer. After further debugging, the culprit turned out to be trailing commas within the data.

{"foo": {
    "bar": [
        {"id": "1", "url": "http://www.hostA.com"},
        {"id": "2", "url": "http://www.hostB.com"},
] }}

theObject.foo.bar.length returns 2 in Firefox, but it returns 3 in Internet Explorer. The solution was obviously to remove the last comma.

I'm guessing the problem is more to Javascript array.length which is handled differently between browsers.

Update: HTML comments embedded within what's supposed to be JSON's data will be ignored by Firefox, while they will cause failure during eval in Internet Explorer.

About these ads

2 Comments

  1. Mike Jones

    Thank you thank you thank you… saved me hours of madness :)

  2. witek

    This is invalid JSON. (but it is valid JavaScript expression). If used as JS term then it is correct, but nobody knows which behaviour (IE or FF) is correct one. :(

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s