• JSON is not just Object Notation

    Aug 4 2006

    Until now, I've just considered JSON to be the equivalent to regular JavaScript object literal notation. Jonathan Snook has now explained the difference. It turns out JSON is a bit more picky than regular object literals. Key names must be quoted, and you can only use double-quotes around keys and values. Also, functions are not allowed as values.

    I gave an example of JSON some time ago that turns out to be wrong. I used single-quotes in my JSON example. I'll leave them there since the example works, but I just can't call it JSON.

    If you're doing JSON-style coding by hand for your Ajax communications, you can get away with using any object literals. So why care if it fits the JSON standard? Well, there are parsers and tools out there which can work with the JSON format, and if you don't fit the standard, they might not work.

    I have no idea why the designers of JSON didn't just make the standard flexible enough to handle single quotes and unquoted keys. It's also confusing that JSON stands for JavaScript Object Notation, and that Object Notation somehow means something different from the object literal notation in JavaScript. Great.

    Moral of the story: it probably doesn't matter if you use the JSON standard until you have to interoperate with JSON tools or feel the need to offer your JSON data to the public. You should probably stop calling it JSON if you use single-quotes, though.

  • Comments

    1. Jonathan Snook at 9:44pm on August 7, 2006

    Just to clarify a couple things that even confused me about JSON.  Despite calling it "object notation", the ECMA spec never uses the term. The object literal is the term the spec uses. Hey, I've used the term object notation, too, as the equivilant of object literal.

    That issue aside, the reason for double quotes and not single quotes likely has to do with language support. For example, valid JSON is valid JavaScript is valid Python. In most other languages, double quotes are used to denote strings. There's a definite consistency in sticking with double quotes.

    2. Jesse Skinner at 4:11am on August 8, 2006

    Yeah, the language support thing makes sense now. The "object notation" thing threw me off too even as I was writing this article - I added the word "literal" afterwards because I wasn't even aware I should be using the word.

    3. Terr at 3:38pm on August 10, 2007

    Also, suppose you have a JSON string in PHP or some other language, and you want to put the string as the value in an HTML form element. When constructing the (x)HTML and the value attribute of the input tag, do you single-quote the JSON? Do you double-quote it? Do you know in advance whether the string you are about to put between quotes has it's own internal quotes which will make for broken HTML?

    Having "double quoted only" is very wise because then nobody has to constantly guess or convert, and you can print the JSON string into the HTML and reliably use single quotes around it.

    4. Jesse Skinner at 11:58pm on March 5, 2008

    @Terr - That's actually rather dangerous. You should be using addslashes() or a similar function to escape single quotes when placing a JSON string inside single quotes.

    Consider the following valid JSON example:

    var json = '{ "key": "this's the value" }';

    As you can see, the single quote inside the string will cause a syntax error.

    5. David Costa at 12:37am on January 16, 2009

    Hi, i was searching some information about JSON for my AJAX platform and when i finish reading this article i concluded that JSON is not even Javascript-only oriented and not Object Notation thing, because u can't set any Javascript Object entirely, for example functions. It is not Javascript-only oriented because it's used by many other languages. It is a good ideia to have data that can be recognized by several programming languages. I'm thinking that JSON is a bad name for that. We can call it Cross Data Notation. In programming if u set good names for objects, classes or even techologies, u can get oriented about what ur doing. So it happens with JSON, as well.

    6. Mark Gonzales at 12:37am on May 12, 2009

    FYI: Key name can be "NOT" qouted, You can use single quotes around key values and YES functions can be use as values.

    7. Jesse Skinner at 10:01am on May 12, 2009

    @Mark - that is true for JavaScript objects, but the JSON standard is more strict. Most JSON parsers will break if you use single quotes, leave off quotes, or use functions.

    If you're only using JavaScript's eval(), then you can just use JavaScript objects including functions.

    Commenting is now closed. Come find me on Twitter.