←back to thread

Fixing JSON

(www.tbray.org)
139 points robin_reala | 10 comments | | HN request time: 0.76s | source | bottom
1. greenyoda ◴[] No.12327716[source]
I never understood why the double quotes around property names are mandatory. For example, in JavaScript, I can write

    { foo: 1, bar: 2 }
but JSON syntax insists on

    { "foo": 1, "bar": 2 }
This makes JSON less easily readable by humans, and harder to write or edit by hand.

Anyone know why JSON was designed this way?

replies(6): >>12327762 #>>12327775 #>>12327805 #>>12327829 #>>12327836 #>>12327945 #
2. jerluc ◴[] No.12327762[source]
I'm no expert, but I'd imagine fully dropping the quotes wouldn't allow you to use the same syntax as a map/dictionary in Javascript, whereby the keys could be [almost] any type, and string keys could contain characters such as - or even : which otherwise would prove ambiguous to a Javascript parser.
3. nemild ◴[] No.12327775[source]
I don't know the history of why design decisions were made in JSON, but one might be the allowance for spaces in keys and mirroring the usage within arrays (where commas and spaces may be used in a string, but shouldn't be confused with a record separator).

Per my other comment, there are a variety of plugins on the editor side that will make JSON look like this (e.g., vim-json ). To write out to JSON from that format, you can obviously just write a plugin (in JS this is one line).

4. Feneric ◴[] No.12327805[source]
The stricter syntax allows JSON to be directly parsed outside of JavaScript in other languages like Python. This is a nice feature and one of the selling points of JSON.
5. shill ◴[] No.12327829[source]
My assumption is that quotes are needed for keys with spaces.

    > x = {}
    > x["foo bar"] = 1
    > x
    { 'foo bar': 1 }
replies(1): >>12334340 #
6. xrstf ◴[] No.12327836[source]
Because using certain keys could break your JavaScript (and JSON was designed to be a subset), like

    { delete: "me" }
If you don't want to keep a list of known keywords around, just insisting on quotes is the easier way.

[EDIT: See also this YouTube video, where Douglas Crockford explains it himself: https://youtu.be/-C-JoyNuQJs?t=5m]

replies(1): >>12328698 #
7. usrusr ◴[] No.12327945[source]
JSON values low variance between different representations of logically equivalent documents. It's not a primary concern (e.g. the explicit lack of key order allows a lot of variance), but it seems to trump syntax convenience just about everywhere except whitespace. Going all unquoted-keys would also be low variance, but this would restrict keys from "any string" to "valid javascript identifiers" or something bespoke in between.
8. yepperino ◴[] No.12328698[source]
Unquoted keywords are valid ES5.1 object properties.

    alert(JSON.stringify({delete: "me", for: 1, var: 2}));
results in:

    {"delete":"me","for":1,"var":2}
The only browser it does not work in is IE8 and earlier.
replies(1): >>12365002 #
9. Retra ◴[] No.12334340[source]
That only explains why they are permitted, not why they are mandatory.
10. lucian1900 ◴[] No.12365002{3}[source]
JSON significantly predates ES 5.1