Do the JSON keys have to be surrounded by quotes?

后端 未结 6 888
生来不讨喜
生来不讨喜 2020-11-22 11:23

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

{
    precision: \"zip\"
}

Or should I always use the following syntax? (And if

相关标签:
6条回答
  • 2020-11-22 11:50

    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.

    0 讨论(0)
  • 2020-11-22 11:54

    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"}.

    0 讨论(0)
  • 2020-11-22 11:54

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

    string
        ""
        " chars "
    
    0 讨论(0)
  • 2020-11-22 12:13

    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.

    0 讨论(0)
  • 2020-11-22 12:14

    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.

    0 讨论(0)
  • 2020-11-22 12:16

    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)

    0 讨论(0)
提交回复
热议问题