4

It seems one of the best-kept secrets of JSON: When exactly can you leave out the quotes around a string – and what quotes (single or double) are you supposed to use anyway?

The JSON standard is pretty clear about it: use double quotes, and use them always. Yet nobody seems to follow that, and parsers seem generally fine with it.

For example, the keys in JSON documents generally don't seem to need quotes. (I guess that's because the parser can assume that the key must be a string literal). But is that an actual rule? Are there any other such rules? Are they parser-specific or language-specific?

Note that although the question is about JSON, this includes the standard way to express JSON objects in a given programming language. If a language (such as JavaScript) has official rules that divert from the JSON standard, it would be helpful to see them defined.

drmirror
  • 3,698
  • 26
  • 26
  • One thing to beware of is confusing the representation of JSON with the representation of the objects that result from *parsing* JSON. In Objective-C, eg, the standard `description` (object dump) format of NSArray/NSDictionary strongly resembles JSON, yet quotes are omitted for strings with no blanks or special characters. This does not mean that the source JSON was that way, or that the JSON parser/generator will read or generate JSON without quotes. – Hot Licks Jul 14 '13 at 19:00
  • Valid point. And just as important is the other way round: the syntax of documents that are parsed _into_ JSON. It is helpful to clarify that these are not actually JSON (particularly in the case of JavaScript, which lends JSON half of its acronym). – drmirror Jul 14 '13 at 19:12
  • 1
    The "standard way to express JSON objects in a given programming language" is as a string containing valid JSON. – hobbs Jul 14 '13 at 19:18
  • 1
    JSON is defined by the (remarkably concise) spec at json.org. That spec requires the quotes. Any data sequence that claims to be JSON is not JSON unless it conforms to that spec. Yes, you might find a parser that is "lax", but if you interchange JSON with anyone else, they are perfectly justified in rejecting your JSON if it does not conform. – Hot Licks Jul 14 '13 at 21:30

2 Answers2

6

Never. Dropping the quotes is legal in literals in JavaScript code, but illegal in JSON. Strings are always quoted, and keys are always strings. "Lax JSON" parsers may exist that accept illegal JSON with unquoted keys or other things, but that doesn't change the fact that it is illegal JSON as such, and no JSON parser is required to accept it.

hobbs
  • 223,387
  • 19
  • 210
  • 288
3

Dropping the quotes in JSON object keys is a feature of the Javascript language, and possibly others. Python, for instance, has a dictionary syntax that is pretty similar to Javascript except that key names cannot be unquoted (though they can be single-quoted, and they don't need to be strings).

May be a duplicate of this question: JSON Spec - does the key have to be surrounded with quotes? And this one: What is the difference between object keys with quotes and without quotes?

Neither of which addresses the question of whether this is in the Javascript specification, or if it is just allowed by most browsers. I found this in the official ECMAScript specification:

The first defines an object literal, in which the PropertyNameAndValue can be a StringLiteral or an IdentifierLiteral. The second defines an IdentifierLiteral, which does not have quotes.

So, yes, unquoted property names are officially allowed in Javascript.

techraf
  • 64,883
  • 27
  • 193
  • 198
Jim Pivarski
  • 5,568
  • 2
  • 35
  • 47
  • The replies to that other question only repeat what's in the standard though. It would be helpful to have more precise rules for each specific language (link?). For example, it seems that in Javascript you can only omit the quotes if the key does not conflict with a Javascript keyword. – drmirror Jul 14 '13 at 17:53
  • Yes, and the IdentifierName is "something without quotes, provided it does not clash with a reserved word". Thanks, good info. – drmirror Jul 14 '13 at 18:19
  • 2
    Unquoted property names are allowed in Javascript but not in JSON. The question is about JSON. – ᄂ ᄀ Jul 14 '13 at 18:22
  • @fnt Disagree. Yes, the question is about JSON, but that includes the standard way to express JSON objects in a given programming language. – drmirror Jul 14 '13 at 18:26
  • Maybe the question needs to be edited, but drmirror and I were talking about JSON-like objects in Javascript and (possibly) other languages. You're right about pure JSON; in the considerably-shorter JSON spec, object property names need to be quoted. – Jim Pivarski Jul 14 '13 at 18:27
  • I have updated the question to clarify that I am indeed also interested in the standard way to express JSON in a given programming language. – drmirror Jul 14 '13 at 18:41
  • @drmirror Could it be that you mix the notion of JavaScript object literal and the one of object representation in JSON? They are not the same. – ᄂ ᄀ Jul 14 '13 at 18:43
  • 2
    @drmirror There is no such thing as "expression of JSON in a given language". JSON relates to JavaScript since the former borrows the syntax to represent objects; apart from that there is no dependence on JavaScript. The format of JSON is independent of the language where it is used. – ᄂ ᄀ Jul 14 '13 at 18:54
  • @fnt It is part of my question to clarify these relationships. The answer that quotes cannot be omitted in JSON is not enough, since people are omitting quotes all the time when dealing with JSON documents, and my question is what exactly is going on there. It is a useful clarification that Javascript has different rules for object literals than JSON has for JSON documents. – drmirror Jul 14 '13 at 19:09
  • 2
    @drmirror no, the statement "people are omitting quotes all the time when dealing with JSON documents" is complete nonsense. – hobbs Jul 14 '13 at 19:14