Why is the slash an escapable character in JSON?

Boldewyn picture Boldewyn · Nov 24, 2010 · Viewed 17.3k times · Source

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?

Answer

aercolino picture aercolino · Dec 29, 2010

I've just published a review of this issue on my blog. I think you are right, that's the only reason. Also note that the slash is the only standard character allowed to be escaped.

Usually JSON encoders do it wrong and escape any slash they find along the way, while only the slash in </script> should be escaped, and maybe all the ones matched by the JavaScript RegExp /<\/\w+/, for the same reason.

EDIT: fixed blog link.