jQuery 1.4 and Malformed JSON

January 15th, 2010

Today, we released jQuery 1.4, a mostly backward compatible release with a few minor quirks. One of these quirks is that jQuery now uses the native JSON parser where available, rejecting malformed JSON.

This probably sounds fine, except that by malformed JSON, we mostly mean:

{foo:"bar"}

Because this is valid JavaScript, people have long assumed it was valid JSON, and a lot of hand-coded JSON is written like this. Thankfully, JSON automatically generated by Rails does not have this problem, so most of you guys should not have an issue here.

If you have server-side JSON that can’t be quickly fixed in the transition to jQuery 1.4, you can use the compatibility plugin, which tries to patch a number of small incompatibilities, or you can use this workaround:

This entry was posted
on Friday, January 15th, 2010 at 12:34 am and is filed under jQuery.
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.

27 Responses to “jQuery 1.4 and Malformed JSON”

Does this new JSON behavior in jQuery 1.4.0 work with Prototype 1.6.x loaded at the same time? I’ve been having all kinds of problems with things that use the native JSON parser when Prototype is in play.

Tho most certainly not the best practice you may want to change that hack to:

json = eval(‘(‘ + text + ‘)’)

…if the user wants to deserialize/unmarshall to an object. Otherwise they’ll just get the first property. Bonus points for checking if JSON object exists and if it does use the JSON.parse method (much safer).

@August, It’s invalid because there are no quotes around foo. See http://json.org/

wycats, Posted January 15, 2010, 4:42 am

Thanks for pointing out the issue with parens. With regard to your second point, you misunderstand the point of my post. jQuery already *natively* uses JSON.parse internally, which breaks malformed JSON that is valid JavaScript. This hack allows you to deserialize such malformed JSON.

Making double quote obligatory makes the JSON data format even simpler. In JavaScript they are optional, so you have three possible code paths: double-quoted, single-quote, or not quoted at all. Making it mandatory means now you only have one code path.

BurningHellfire, Posted February 16, 2010, 7:13 pm

This change is totally bullshit. The whole beauty of JSON was that you return a string and it just gets evaled. Why do I want to waste the user’s resources to fucking parse a string to make sure it is valid first? I’m the one sending the string, it’s my own goddamn fault if it’s invalid. What exactly is the benefit of being forced to use more quotes? It just eats bandwidth unnecessarily. FUCK THAT!

thomas, Posted April 3, 2010, 7:14 am

I’m fucked ^^

I just adjusted my entire code to json, today I update to 1.4 and none of my json strings work ^^ damn quotes :D

Phil, Posted May 28, 2010, 1:29 am

I have to say that I agree this change is total bullshit. Changing working functionality just to make your life easier coding the library is never a good idea.

This is just the final nail in the coffin for me JQuery is the Visual basic of JavaScript frameworks.

Kirby L. Wallace, Posted June 14, 2010, 3:47 pm

Wow. That turned nasty real fast.

Pardon me, but did he say there was a compatibility plugin you can use if you simply insist on bastardising the JSON standard? I’m not real into JSON (yet – but getting there), so I may be wrong.