the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • 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.

    Tags: javascript links snook
    View 4 Comments | Add a comment
  • Comments

    1. Jonathan Snook at 3:44am on August 8, 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 10: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 9: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 5:58am on March 6, 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.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Google is Hosting Ajax Libraries
    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • See All...
  • Categories

    • javascript (37)
    • links (20)
    • about (19)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites