This isn't the same thing.  Consider the following code:

var json1 = { jack: "jack jackson" };
var json2 = '{ sara: "sara jones" }';

In this case, json1 becomes a javascript object in memory. json2 is a string that just so happens to look like a javascript object.

In the first case, this is valid because JS handles things properly. This means you can use quotes or not for the property name - both work. (unless you have spaces or other special characters in the property name).

In the second case, the string will not get converted to a javascript object reliably in all cases. Because it is not a "standard" json string.

Perhaps I mis-understood the original question, but I thought we were talking about instances where we were defining strings, in which case quoting is healthier.

I personally only use the quoted property names when I am building json strings - whether server side, or client side. But if I just need to define an object client side, I don't usually bother.

So, I guess the distinction is what your target data type is - string or object. With string, always quote. With object, it's optional.

My thoughts.

Shawn


unwiredbrain wrote:
Actually, quoting is optional *EXCEPT* if member name contains spaces
or is a language-specific reserved word.

Try to jslint this fragment:

var JSONTest = {
    "jack johnson": "john jackson",
    "alice": "bob",
    "var": "error"
};

Unquoting alice makes no difference; doing the same for jack johnson
or var throws an error, but that's because we're dealing with
JavaScript: other languages could yeld errors on different reserved
words.

Then, in order to pre-emptively avoid such problems, my advice is to
*always* use member quoting.

Reply via email to