2

For javascript object, for its property, I usually do not add a single quote to it. I remember if I add a single quote to an object' property, it will become a JSON? am I right?

Please see this code, both code will output object's value. Please explain do I need to add a single quote to a object's property?

var test1 = {
    foo: 'foo1',
  bar: 'bar1'
};
alert(test1.foo);

var test2 = {
    'foo': 'foo1',
  'bar': 'bar1'
};
alert(test2.foo);

https://jsfiddle.net/azp9wj0x/

user8565199
  • 133
  • 2
  • 8
  • JSON uses double quotes for property names. Note even if you used them here, it still wouldn't be JSON it would still be an object literal. See [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Patrick Evans Sep 18 '17 at 19:17
  • It will still be a JavaScript object literal in both cases. You don't need the single quotes, but if for some reason you have a property name that violates standard naming convention (like spaces), you can use quotes around it. – lintmouse Sep 18 '17 at 19:17
  • single quotes are literal string. double quotes are used when values contain special characters such as "\n" – schylake Sep 18 '17 at 19:17
  • double quotes are standard http://www.json.org/ – sumeet kumar Sep 18 '17 at 19:17
  • @schylake double and single quotes in JavaScript are exactly the same and special characters can be included in strings in exactly the same way regardless of the outer quotes used. – Pointy Sep 18 '17 at 19:18

2 Answers2

2

Remember that JSON is only tangentally related to JavaScript and is a spec separate from JavaScript itself.

Single quotes are not valid JSON. Double quotes are. This is irrelevant, though, because this is JavaScript code and it's much more forgiving in terms of how you define things.

JSON only comes into play when you're serializing things for transport outside of your application. In that case it really doesn't matter how you define your data:

var test1 = {
  foo: 'foo1',
  bar: 'bar1'
};

JSON.stringify(test1, null, 2);

That will properly encode as JSON regardless of your use of single, double, or no quotes at all.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • "Single quotes are not valid JSON" But If I change "name" to 'name', this (https://www.w3schools.com/js/tryit.asp?filename=tryjson_send) still can read the name. Please explain. Thanks. – user8565199 Sep 18 '17 at 19:33
  • That converts a JavaScript object **to** JSON. What you are writing isn't JSON. – Quentin Sep 18 '17 at 19:37
  • @user8565199 Verify JSON with a [JSON validator](http://jsonlint.com), not JavaScript. You're also going to get into trouble *fast* by stapling arbitrary JSON onto the end of a GET request. You should use POST. – tadman Sep 18 '17 at 20:02
2

Property names in an object literal in ES5 can be identifiers or string literals (ES6 adds some more options but that's by the by).

Without quotes, it is an identifier. With quotes, it is a string.

Using a string allows you to use property names that are not valid identifiers (such as those with a space in them).

This has nothing to do with JSON. JavaScript is JavaScript. JSON is JSON.


JSON is a data format inspired by JavaScript literal syntax. In JSON property names must be strings and strings must be delimited with " characters.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335