KishCom

Developer. Gamer. Yo-Yo Thrower.

JSON Gotcha!

Working with web tech all day, every day means I get to learn lots of very neat tricks that aren't in regular use. It also means I run up against bugs so hard to track down it borders on literal insanity. This is the story of one of those bugs.
"JSON is a Javascript subset" you've heard them say. That all properly formatted JSON will parse properly. Well they LIED to you. Open your Firebug or Chrome Inspector and Copy/Paste the following snippit (copy/paste is important) and execute it:
var someJSONObj = ({"Derp":"Le 
herp"});
If you copy/pasted you'll get an error along the lines of "Unterminated string literal" or "Unexpected token ILLEGAL". Why you ask? Why? Because of a single whitespace character I slipped in there right before the "h" - "Line Separator (U+2028)" and "Paragraph Separator (U+2029)" both will cause this error. Even though the JSON specification says you can use these two quirky whitespace characters - they fail in the browser. The solution is to use their escape sequences like this: \u2028 and \u2029 -- it's doubtful that you'll ever even run into this in most web apps, but it's still a weird little "gotcha".
Back to KishCom.com
KishCom.Com

Table of Contents