-1

Let's say I have a rather simple method as shown below:

var foo = function (str) {
    console.log(str); //{"Field Name": "Value With "Escaped" Content"}
    JSON.parse(str); //fails
} 

foo('{"Field Name": "Value With \"Escaped\" Content"}');

The passed argument features intentional backslashes (the string originates from a third party), however, they're absent once the string is passed to the function (see the inline comments).

Can I avoid the issue without manually double-escaping the string? Why doesn't using single slashes around the string itself avoid this issue? Is it possible to retrieve the raw input string?

Etheryte
  • 24,589
  • 11
  • 71
  • 116
  • you escape `"` because in this context you are declaring a string (btw useless because surrounded by simple quotes), you are not creating escaped double quotes – Hacketo Sep 25 '15 at 12:46
  • possible duplicate of [Get backslashes inside a string - Javascript](http://stackoverflow.com/questions/10041998/get-backslashes-inside-a-string-javascript) – Hacketo Sep 25 '15 at 12:51
  • Are you just copy and pasting this JSON into your source code for testing, but in practice the string will come from some API? Then you won't even have this problem in practice! Otherwise, clarify where that string comes from and what you do with it. – deceze Sep 25 '15 at 13:06
  • @deceze The function is called with the parameter inlined by a third-party script, I can simply modify the receiving function. – Etheryte Sep 25 '15 at 13:09
  • And this third party script is unable to correctly escape strings to conform to Javascript syntax? – deceze Sep 25 '15 at 13:09
  • @deceze I wouldn't be asking this question if that wasn't the case, so yes. – Etheryte Sep 25 '15 at 13:10
  • 2
    Then I think you're mostly screwed. – deceze Sep 25 '15 at 13:11

1 Answers1

2

The \ characters are JSON escape characters but they are also JavaScript escape characters.

You have a JavaScript string literal. When that is parsed by the JavaScript compiler the \" will be interpreted as ".

The JSON parser therefore gets passed " and not \".

Can I avoid the issue without manually double-escaping the string?

The string needs to be double escaped. You may or may not have to do that manually depending on where the string comes from.

Why doesn't using single slashes around the string itself avoid this issue?

Because the language design of JavaScript doesn't say that \ characters are not escape characters in single quoted strings.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Is it possible to retrieve the raw input string without it being parsed? – Etheryte Sep 25 '15 at 12:58
  • @Nit linked post state `String.raw` ES6 – Hacketo Sep 25 '15 at 13:04
  • 1
    Only by accessing the raw JavaScript and writing a custom parser. (That would be a stupid approach to solving the problem). – Quentin Sep 25 '15 at 13:04
  • @Nit It is being "parsed" the second you type it into the source code (well, as soon as it is evaluated). The string `"foo\"bar"` in Javascript source code really means `foo"bar`. – deceze Sep 25 '15 at 13:05
  • @deceze I understand that, but I was wondering if accessing it is still possible, looks like `String.raw` answers that question, but that's ES6. – Etheryte Sep 25 '15 at 13:07