Do the JSON keys have to be surrounded by quotes?

JsonSpecificationsStandards ComplianceJson5

Json Problem Overview


Example: Is the following code valid against the JSON Spec?

{
    precision: "zip"
}

Or should I always use the following syntax? (And if so, why?)

{
    "precision": "zip"
}

I haven't really found something about this in the JSON specifications. Although they use quotes around their keys in their examples.

Json Solutions


Solution 1 - Json

Yes, you need quotation marks. This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"}.

Solution 2 - Json

You are correct to use strings as the key. Here is an excerpt from RFC 4627 - The application/json Media Type for JavaScript Object Notation (JSON)

> 2.2. Objects > > > An object structure is represented as a pair of curly brackets > surrounding zero or more name/value pairs (or members). A name is a > string. A single colon comes after each name, separating the name > from the value. A single comma separates a value from a following > name. The names within an object SHOULD be unique. > > object = begin-object [ member *( value-separator member ) ] > end-object > > member = string name-separator value > > [...] > > 2.5. Strings > > The representation of strings is similar to conventions used in the C > family of programming languages. A string begins and ends with > quotation marks. [...] > > string = quotation-mark *char quotation-mark > > quotation-mark = %x22 ; "

Read the whole RFC here.

Solution 3 - Json

From 2.2. Objects

> An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string.

and from 2.5. Strings

> A string begins and ends with quotation marks.

So I would say that according to the standard: yes, you should always quote the key (although some parsers may be more forgiving)

Solution 4 - Json

Yes, quotes are mandatory. http://json.org/ says:

string
    ""
    " chars "

Solution 5 - Json

Yes they do. But if you need otherwise, checkout JSON5.

JSON5 is a superset of JSON that allows ES5 syntax, including:

  • unquoted property keys
  • single-quoted, escaped and multi-line strings
  • alternate number formats
  • comments
  • extra whitespace

The JSON5 reference implementation (json5 npm package) provides a JSON5 object that has parse and stringify methods with the same args and semantics as the built-in JSON object.

Solution 6 - Json

In your situation, both of them are valid, meaning that both of them will work.

However, you still should use the one with quotation marks in the key names because it is more conventional, which leads to more simplicity and ability to have key names with white spaces etc.

Therefore, use the one with the quotation marks.

edit// check this: https://stackoverflow.com/q/2904131

Solution 7 - Json

Since you can put "parent.child" dotted notation and you don't have to put parent["child"] which is also valid and useful, I'd say both ways is technically acceptable. The parsers all should do both ways just fine. If your parser does not need quotes on keys then it's probably better not to put them (saves space). It makes sense to call them strings because that is what they are, and since the square brackets gives you the ability to use values for keys essentially it makes perfect sense not to. In Json you can put...

>var keyName = "someKey";
>var obj = {[keyName]:"someValue"};

>obj
Object {someKey: "someValue"}

just fine without issues, if you need a value for a key and none quoted won't work, so if it doesn't, you can't, so you won't so "you don't need quotes on keys". Even if it's right to say they are technically strings. Logic and usage argue otherwise. Nor does it officially output Object {"someKey": "someValue"} for obj in our example run from the console of any browser.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchristianvueringsView Question on Stackoverflow
Solution 1 - JsoncobbalView Answer on Stackoverflow
Solution 2 - JsonPatrikAkerstrandView Answer on Stackoverflow
Solution 3 - JsonCebjyreView Answer on Stackoverflow
Solution 4 - JsonlutzView Answer on Stackoverflow
Solution 5 - JsonInigoView Answer on Stackoverflow
Solution 6 - JsonwolfenblutView Answer on Stackoverflow
Solution 7 - JsonMaster JamesView Answer on Stackoverflow