Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the slash an escapable character in JSON? [duplicate]

Tags:

Possible Duplicate:
JSON: why are forward slashes escaped?

json.org states, that forward slashes (aka solidus, /) can be escaped:

"\/" 

However, unescaped slashes are valid, too:

"/" 

What's the rational behind this? Does it come from the Javascript roots? (I.e., "</script>" is a problem in browser-based Javascript, see Douglas Crockford's comment) Or has it any other reason?

like image 610
Boldewyn Avatar asked Nov 24 '10 08:11

Boldewyn


People also ask

Why is slash escaped in JSON?

This is because HTML does not allow a string inside a <script> tag to contain </ , so in case that substring's there, you should escape every forward slash.

Is backslash valid in JSON?

The following characters are invalid when used in a JSON key: " (double quote) – It must be escaped. \ (backslash) – It must be used to escape certain characters. all control characters like \n , \t.

Why does JSON escape solidus?

JSON only allows a select few characters to be escaped to avoid ambiguities in the way eval works to unpack JSON -- '\v' is a vertical tab in most interpreters but the letter 'v' in others. The solidus is among the set of characters that MAY be escaped so that JSON can be embedded.


1 Answers

It seems, my first thought was correct.

'\/' === '/' in JavaScript, and JSON almost is valid JavaScript. However, why are the other ignored escapes (like \z) not allowed in JSON?

The key for this was reading http://www.cs.tut.fi/~jkorpela/www/revsol.html, followed by http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2. The feature of the slash escape allows JSON to be embedded in HTML (as SGML) and XML.

like image 57
Boldewyn Avatar answered Oct 10 '22 05:10

Boldewyn