Why does the jQuery JSON parser need double escaping for backslashes?

The first escape escapes it in the Javascript string literal.
The second escape escapes it in the JSON string literal.

The Javascript expression '{"a":"b:\\c"}' evaluates to the string '{"a":"b:\c"}'.
This string contains a single unescaped \, which must be escaped for JSON. In order to get a string containing \\, each \ must be escaped in the Javascript expression, resulting in "\\\\".

Leave a Comment