Are there any forbidden characters in key names, for JavaScript objects or JSON strings? Or characters that need to be escaped?
To be more specific, I'd like to use "$", "-" and space in key names.
Are there any forbidden characters in key names, for JavaScript objects or JSON strings? Or characters that need to be escaped?
To be more specific, I'd like to use "$", "-" and space in key names.
No. Any valid string is a valid key. It can even have "
as long as you escape it:
{"The \"meaning\" of life":42}
There is perhaps a chance you'll encounter difficulties loading such values into some languages, which try to associate keys with object field names. I don't know of any such cases, however.
The following characters must be escaped in JSON data to avoid any problems:
"
(double quote)\
(backslash)\n
, \t
JSON Parser can help you to deal with JSON.
It is worth mentioning that while starting the keys with numbers is valid, it could cause some unintended issues.
Example:
var testObject = {
"1tile": "test value"
};
console.log(testObject.1tile); // fails, invalid syntax
console.log(testObject["1tile"]; // workaround
Unicode codepoints U+D800 to U+DFFF must be avoided: they are invalid in Unicode because they are reserved for UTF-16 surrogate pairs. Some JSON encoders/decoders will replace them with U+FFFD. See for example how the Go language and its JSON library deals with them.
So avoid "\uD800" to "\uDFFF" alone (not in surrogate pairs).
Both JSON and JavaScript allow arbitrary strings as object property names, according to their own language definitions. The most recent JSON language definition documents are RFC 8259 for JSON and ECMA-262 for JavaScript.
The characters needing escaping in keys are the characters that are required to be escaped in any string in the language. These are also given in the language definition documents. For JSON, the characters required to be escaped are the quotation mark, backslash, and control characters. For Javascript, the characters requiring escaping are the quote character matching the enclosing quotes (single or double), backslash, carriage return, and line feed.
For your specific example, all of "$", "-" and space are allowed as keys of both JSON & JavaScript objects with no escaping required.
Per RFC 8259, there are no limits imposed on the value of strings used as JSON object names:
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
Regarding JavaScript, ECMA-262 explicitly states that all strings are valid object property names:
A property key value is either an ECMAScript String value or a Symbol value. All String and Symbol values, including the empty String, are valid as property keys. A property name is a property key that is a String value.
RFC 8259 lists the characters that must be escaped in JSON:
All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
ECMA-262 lists the characters that must be escaped in JavaScript:
A string literal is 0 or more Unicode code points enclosed in single or double quotes. […] All code points may appear literally in a string literal except for the closing quote code points, U+005C (REVERSE SOLIDUS), U+000D (CARRIAGE RETURN), and U+000A (LINE FEED).
The closing quote code point would be "
if the string is enclosed in double quotes, and '
if it is enclosed in single quotes.