-1

Many questions ask what's the difference between

obj = {"foo" : "bar"} 

and

obj = {foo: "bar"}

And the answer is that quotes are proper JSON syntax while no-quotes is Javascript syntactic sugar. My question is, how can I take any unquoted/semi-quoted javascript object ie:

semi = { "foo" : "bar", hello: "world"}

and return a fully quoted object ie:

fully = { "foo" : "bar", "hello": "world" }

In Javascript. I don't mean JSON.stringify() as that would return a string, I still want a normal JS object. Thanks!

rausted
  • 951
  • 5
  • 21
  • What is your purpose? Both are interpreted the same way in js. By that i mean that your semi and fully object are the same in all mesures, you can look in you console – Ulysse BN Jan 30 '17 at 01:01
  • Quotes are optional so long as property names don't contain spaces or special characters and don't start with numbers. Not clear what you are trying to accomplish – charlietfl Jan 30 '17 at 01:02
  • 1
    Your question doesn't make any sense. The use of strings or identifiers only exist in the JavaScript source code. The object you get out of them is just an object with properties. The syntax used to construct it is irrelevant. You can't get the original source code out of an object. – Quentin Jan 30 '17 at 01:04
  • 1
    The values of `semi` and `fully` are exactly equivalent; the use of quoted keys is just a syntax thing, and doesn't affect the resulting object. Why do you want to do this? – qxz Jan 30 '17 at 01:04
  • 2
    "And the answer is that quotes are proper JSON syntax" — No. The answer is "There is no difference" (because you aren't writing JSON so JSON is irrelevant … and if you were writing JSON then `obj =` would make it invalid) – Quentin Jan 30 '17 at 01:05
  • 1
    A JavaScript object's property is not quoted or unquoted. The quote is only used for syntax when the JavaScript engine parses your code. Therefore, what you are asking does not make sense. – Ruan Mendes Jan 30 '17 at 01:08
  • I mean... *technically* you *could* do `{ '"foo"': "bar" }`... – Jordan Running Jan 30 '17 at 02:52
  • Still though, if you print the two objects, semi and fully, one is gonna show with quotes and the other with no quotes. So as far as javascript is concerned, there is some semantic differences there. So I think I should be able to change if I want quotes or no quotes. If I print the objects with JSON.stringify() they are the same, I agree. – rausted Jan 30 '17 at 03:57

2 Answers2

2

The Javascript interpreter quotes properties when they're being displayed and only if they contain dashes, spaces or other special characters.

Javascript objects are NOT the same thing as JSON. They may look very similar, but JSON is a string representation of a javascript object, while a javascript object has actual properties with property names. That's why you can access those properties with dot syntax.

console.log( semi.foo );

The string representation with quotes is just for your visual benefit and not how they're represented internally.

Soviut
  • 88,194
  • 49
  • 192
  • 260
0

JavaScript object keys are not quoted strings: they are simply primitive string values.

Key definition in source code using

{ unquotedKey:  "some value" }

as an object initializer creates and initializes an object with a key-value pair. The syntax does, however, require unquotedKey to conform to the rules of JavaScript identifiers.

Key definition in source code using

{ "quoted value!": "some value" }

in a means of creating an object with a key that does not conform to the rules of JavaScript identifiers.

The same restrictions apply when accessing JavaScript property names using shorthand dot notation: foo.bar works because bar is an identifier, but foo["was here"] requires array lookup notation because was here is not an identifier.

Now enter JSON.stringify. The stringify method quotes all property names by default, for the purpose of interchanging a string representation of objects between systems and programming languages. It is a misinterpretation to infer from JSON.stringiy's behavior that quoted identifier names are somehow better, or the proper way to write an object literal in JavaScript with property names designed for programmatic access.

traktor
  • 17,588
  • 4
  • 32
  • 53