←back to thread

Fixing JSON

(www.tbray.org)
139 points robin_reala | 1 comments | | HN request time: 0.39s | source
Show context
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 #
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 #
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 #
1. lucian1900 ◴[] No.12365002[source]
JSON significantly predates ES 5.1