JSON requires double quotes around keys whereas JavaScript does not. Modern browsers have a built-in global object JSON with encoding and decoding functions.
It can be either. Both strings and numbers are valid JSON values.
JSON names require double quotes.
→ JSON is basically the “String” version of JavaScript Object. Its simply a text-based data format and single quote is not allowed for JSON. → 6 types of data types are allowed to contain in a JSON data/string/text.
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"}
.
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.
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)
Yes, quotes are mandatory. http://json.org/ says:
string
""
" chars "
Yes they do. But if you need otherwise, checkout JSON5.
JSON5 is a superset of JSON that allows ES5 syntax, including:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With