Impostor JSON

This tip will definitely help some of you out.

When creating JSON-formatted data, it seems as if you can omit quoting the keys:

{ name: "Anthony", reading: "The Wisdom Of Crowds" }

…although technically this is not JSON, as the key values are not quoted. However, you’ll see lots of examples floating around that use the non-key-quoting system. And it works in a lot of cases.

However, I ran into this code in Douglas Crockford’s JSON library for Javascript that was failing:

if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { /* ... */ }

Ha! Ha! Ha! Figuring out the error in this code is sort of like figuring out what smells in a landfill. But, it was easier than I thought. What was happening was that the string I was sending in to parse to JSON did not have the keys wrapped in double quotes, so the Crockford script failed, thinking it was not a valid JSON string.


  1. Firebug is your friend.
  2. Type safety is your friend.
  3. Generic “syntax error” exceptions are not your friend, particularly when you’re chaining four calls together in one line.

2 Responses to “Impostor JSON”

  1. 1 Douglas Crockford February 21, 2008 at 2:33 pm

    The role of is not to be a developer’s friend. Its role is to defend users from developers.

    The developer’s friend is JSLint. It would have told you exactly what your problem was.

  1. 1 YSlow and JLint « The Pursuit of a Life Trackback on February 21, 2008 at 8:02 pm

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

TwitterCounter for @anthonyrstevens
Add to Technorati Favorites

RSS Feed

View Anthony Stevens's profile on LinkedIn

%d bloggers like this: